Linkedin URL验证 – 输入字段JS

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

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的模式匹配,就像这个演示一样:

  1. $("#validateurl").click(function(){
  2. pattern = new RegExp(/(https?)?:?(\/\/)?(([w]{3}||\w\w)\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&%@\!\/-\/]))?/);
  3. if(!pattern.test($("#urlvalue").val())) {
  4. alert("Url not valid");
  5. } else {
  6. alert("Valid url");
  7. }
  8. });
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <input type="text" id="urlvalue" />
  3. <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 -->

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

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

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

<!-- end snippet -->

Examples of valid URLs:

  1. http://linkedin.com/in/jjjjjj
  2. https://www.linkedin.com/company/somecompany/

Examples of not valid URLs:

  1. http://test.com
  2. http://www.aaa.com/444

答案2

得分: 0

  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <input type="text" id="UrlVal" />
  3. <button id="BtnCheck"> Check </button>
  1. $("#BtnCheck").click(function(){
  2. val = $('#UrlVal').val();
  3. if( /(ftp|http|https):\/\/?(?:www\.)?linkedin.com(\w+:{0,1}\w*@)?(\S+)(:([0-9])+)?(\/|\/([\w#!:.?+=&amp;%@!\-\/]))?/.test(val) )
  4. {
  5. alert( 'valid Linkedin URL' );
  6. }
  7. else
  8. {
  9. alert( 'not valid Linkedin URL' );
  10. }
  11. });
英文:

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

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

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

  1. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;input type=&quot;text&quot; id=&quot;UrlVal&quot; /&gt;
  3. &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:

确定