英文:
Linkedin url Validation - Input field JS
问题
有一个非常简单的表单,其中有一个输入字段用于LinkedIn链接。有没有办法验证该字段只接受LinkedIn链接?谢谢。
英文:
i have a from just simple form there is an input field for Linkedin url. Is there is anyway to validate that field only accepts linkedin url? Thanks
答案1
得分: 0
使用JS
可以检查输入值是否与LinkedIn URL的模式匹配,就像这个演示一样:
$("#validateurl").click(function(){
pattern = new RegExp(/(https?)?:?(\/\/)?(([w]{3}||\w\w)\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@\!\/-\/]))?/);
if(!pattern.test($("#urlvalue").val())) {
alert("Url not valid");
} else {
alert("Valid url");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="urlvalue" />
<button id="validateurl"> Validate </button>
有效URL示例:
无效URL示例:
英文:
Using JS
you can check if the input value matches a pattern for a LinkedIn URL, Like this demo:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("#validateurl").click(function(){
pattern = new RegExp(/(https?)?:?(\/\/)?(([w]{3}||\w\w)\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/);
if(!pattern.test($("#urlvalue").val())) {
alert("Url not valid");
} else {
alert("Valid url");
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="urlvalue" />
<button id="validateurl"> Validate </button>
<!-- end snippet -->
Examples of valid URLs:
http://linkedin.com/in/jjjjjj
https://www.linkedin.com/company/somecompany/
Examples of not valid URLs:
http://test.com
http://www.aaa.com/444
答案2
得分: 0
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="UrlVal" />
<button id="BtnCheck"> Check </button>
$("#BtnCheck").click(function(){
val = $('#UrlVal').val();
if( /(ftp|http|https):\/\/?(?:www\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(val) )
{
alert( 'valid Linkedin URL' );
}
else
{
alert( 'not valid Linkedin URL' );
}
});
英文:
Using JS you can check if the input value matches a pattern for a LinkedIn URL.I hope solve your problem:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$("#BtnCheck").click(function(){
val = $('#UrlVal').val();
if( /(ftp|http|https):\/\/?(?:www\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test(val) )
{
alert( 'valid Linkedin URL' );
}
else
{
alert( 'not valid Linkedin URL' );
}
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="UrlVal" />
<button id="BtnCheck"> Check </button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论