Linkedin URL验证 – 输入字段JS

huangapple go评论95阅读模式
英文:

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 -->

$(&quot;#validateurl&quot;).click(function(){
  pattern = new RegExp(/(https?)?:?(\/\/)?(([w]{3}||\w\w)\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&amp;%@!\-\/]))?/);
  if(!pattern.test($(&quot;#urlvalue&quot;).val())) {
    alert(&quot;Url not valid&quot;);
  } else {
    alert(&quot;Valid url&quot;);
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input type=&quot;text&quot; id=&quot;urlvalue&quot; /&gt;
&lt;button id=&quot;validateurl&quot;&gt; Validate &lt;/button&gt;

<!-- 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#!:.?+=&amp;%@!\-\/]))?/.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 -->

$(&quot;#BtnCheck&quot;).click(function(){
  val = $(&#39;#UrlVal&#39;).val();
  if( /(ftp|http|https):\/\/?(?:www\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&amp;%@!\-\/]))?/.test(val) )
  {
   alert( &#39;valid Linkedin URL&#39; );
  }
  else
  {
   alert( &#39;not valid Linkedin URL&#39; );
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input type=&quot;text&quot; id=&quot;UrlVal&quot; /&gt;
&lt;button id=&quot;BtnCheck&quot;&gt; Check &lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 18:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定