jquery validation 用到的输入验证正则表达式-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

jquery validation 用到的输入验证正则表达式

简介:

使用方式如:

function checkIsNumber(selector) {
    return !/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test($(selector).val())
}

// http://docs.jquery.com/Plugins/Validation/Methods/minlength
  minlength: function(value, element, param) {
   return this.optional(element) || this.getLength($.trim(value), element) >= param;
  },
  
  // http://docs.jquery.com/Plugins/Validation/Methods/maxlength
  maxlength: function(value, element, param) {
   return this.optional(element) || this.getLength($.trim(value), element) <= param;
  },
  
  // http://docs.jquery.com/Plugins/Validation/Methods/rangelength
  rangelength: function(value, element, param) {
   var length = this.getLength($.trim(value), element);
   return this.optional(element) || ( length >= param[0] && length <= param[1] );
  },
  
  // http://docs.jquery.com/Plugins/Validation/Methods/min
  min: function( value, element, param ) {
   return this.optional(element) || value >= param;
  },
  
  // http://docs.jquery.com/Plugins/Validation/Methods/max
  max: function( value, element, param ) {
   return this.optional(element) || value <= param;
  },
  
  // http://docs.jquery.com/Plugins/Validation/Methods/range
  range: function( value, element, param ) {
   return this.optional(element) || ( value >= param[0] && value <= param[1] );
  },
  
  // http://docs.jquery.com/Plugins/Validation/Methods/email
  email: function(value, element) {
   // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
   return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(http://www.cnblogs.com/yelaiju/admin/file://([/x01-/x09/x0b/x0c/x0d-/x7f]%7C[/u00A0-/uD7FF/uF900-/uFDCF/uFDF0-/uFFEF]))))*(((/x20%7C/x09)*(/x0d/x0a))?(\x20|\x09)+)?(\x22)))@((([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])))\.?$/i.test(value);
  },
 
  // http://docs.jquery.com/Plugins/Validation/Methods/url
  url: function(value, element) {
   // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
   return 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(value);
  },
        
  // http://docs.jquery.com/Plugins/Validation/Methods/date
  date: function(value, element) {
   return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
  },
 
  // http://docs.jquery.com/Plugins/Validation/Methods/dateISO
  dateISO: function(value, element) {
   return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value);
  },
 
  // http://docs.jquery.com/Plugins/Validation/Methods/number
  number: function(value, element) {
   return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
  },
 
  // http://docs.jquery.com/Plugins/Validation/Methods/digits
  digits: function(value, element) {
   return this.optional(element) || /^\d+$/.test(value);
  },
  


本文转自火地晋博客园博客,原文链接:http://www.cnblogs.com/yelaiju/archive/2010/05/28/1746471.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章