javascript – URL的html5标记验证

栏目: Html5 · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/13820477/html5-input-tag-validation-for-url
我试图使用 JavaScript

验证URL但由于某种原因它无法正常工作.当有人没有输入任何URL时,它会显示消息

“请输入有效的URL.(即http://)”

我试图解决它从过去4小时,但它无法正常工作

is there any trick in html5 that validate the URL?

请帮我解决这个问题

$(document).ready(function() { 
$("#contactInfos").validate( 
 { onfocusout: false, rules: { 
       phone: { number:true }, 
       zipcode: { number:true }, 
       website: {    url:true    } 
 }, 
     messages: { phone: { number:"please enter digit only" }, 
     zipcode: { number:"Plese enter digit only"  }, 
     website: { url: "Please enter valid URL.(i.e. http://)"     } 
   } 

});

验证url的方法

url: function(value, element) {
      values=value.split(',');
      for (x in values)
      {
          temp=values[x].trim();
  temp1=this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(temp);

      if(temp1!=true)
      {
        return false;
      }
      }
       return true;
},

谢谢

在html5中,您可以使用标签输入type =“url”:

<input type="url" />

你也可以使用你自己的模式:

<input type="url" pattern="https?://.+" required />

在统一资源标识符(URI):通用语法[RFC3986] http://www.ietf.org/rfc/rfc3986.txt 中,URI的正则表达式是:

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
For example, matching the above expression to
06003
results in the following subexpression matches:
06004

翻译自:https://stackoverflow.com/questions/13820477/html5-input-tag-validation-for-url


以上所述就是小编给大家介绍的《javascript – URL的html5标记验证》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning PHP 5

Learning PHP 5

David Sklar / O'Reilly / July, 2004 / $29.95

Learning PHP 5 is the ideal tutorial for graphic designers, bloggers, and other web crafters who want a thorough but non-intimidating way to understand the code that makes web sites dynamic. The book ......一起来看看 《Learning PHP 5》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具