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

时间:2021-03-01 19:45:03

使用方式如:

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);
  },