评论

收藏

[jQuery] 【jQuery系列之插件】jquery插件之jquery-validation

开发技术 开发技术 发布于:2021-07-30 23:16 | 阅读数:514 | 评论:0

equalTo方法:
1 equalTo: function( value, element, param ) {
 2 
 3       // Bind to the blur event of the target in order to revalidate whenever the target field is updated
 4       var target = $( param );
 5       if ( this.settings.onfocusout && target.not( ".validate-equalTo-blur" ).length ) {
 6         target.addClass( "validate-equalTo-blur" ).on( "blur.validate-equalTo", function() {
 7           $( element ).valid();
 8         } );
 9       }
10       return value === target.val();
11     },
url验证方法:
1 url: function( value, element ) {
2 
3       // Copyright (c) 2010-2013 Diego Perini, MIT licensed
4       // https://gist.github.com/dperini/729294
5       // see also https://mathiasbynens.be/demo/url-regex
6       // modified to allow protocol-relative URLs
7       return this.optional( element ) || /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test( value );
8     },
日期date验证方法:
1 // http://jqueryvalidation.org/date-method/
2     date: function( value, element ) {
3       return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() );
4     },
十进制数字含小数验证方法:
1 // http://jqueryvalidation.org/number-method/
2     number: function( value, element ) {
3       return this.optional( element ) || /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( value );
4     },
整数验证方法:
1 // http://jqueryvalidation.org/digits-method/
2     digits: function( value, element ) {
3       return this.optional( element ) || /^\d+$/.test( value );
4     },
最小长度验证方法:
1 // http://jqueryvalidation.org/minlength-method/
2     minlength: function( value, element, param ) {
3       var length = $.isArray( value ) ? value.length : this.getLength( value, element );
4       return this.optional( element ) || length >= param;
5     },
最大长度验证方法:
1 // http://jqueryvalidation.org/maxlength-method/
2     maxlength: function( value, element, param ) {
3       var length = $.isArray( value ) ? value.length : this.getLength( value, element );
4       return this.optional( element ) || length <= param;
5     },
长度范围验证方法:
1 // http://jqueryvalidation.org/rangelength-method/
2     rangelength: function( value, element, param ) {
3       var length = $.isArray( value ) ? value.length : this.getLength( value, element );
4       return this.optional( element ) || ( length >= param[ 0 ] && length <= param[ 1 ] );
5     },
最小值,最大值,区间值验证方法:
1 // http://jqueryvalidation.org/min-method/
 2     min: function( value, element, param ) {
 3       return this.optional( element ) || value >= param;
 4     },
 5 
 6     // http://jqueryvalidation.org/max-method/
 7     max: function( value, element, param ) {
 8       return this.optional( element ) || value <= param;
 9     },
10 
11     // http://jqueryvalidation.org/range-method/
12     range: function( value, element, param ) {
13       return this.optional( element ) || ( value >= param[ 0 ] && value <= param[ 1 ] );
14     },
两个中文字符验证:自定义方法
1 jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
2   var length = value.length;
3   for(var i = 0; i < value.length; i++){
4     if(value.charCodeAt(i) > 127){
5       length++;
6     }
7   }
8   return this.optional(element) || ( length >= param[0] && length <= param[1] );   
9 }, $.validator.format("请确保输入的值在{0}-{1}个字节之间(一个中文字算2个字节)"));
邮政编码验证:自定义方法
1 // 邮政编码验证   
2 jQuery.validator.addMethod("isZipCode", function(value, element) {   
3   var tel = /^[0-9]{6}$/;
4   return this.optional(element) || (tel.test(value));
5 }, "请正确填写您的邮政编码");
验证提示信息:
1 messages: {
 2     required: "This field is required.",
 3     remote: "Please fix this field.",
 4     email: "Please enter a valid email address.",
 5     url: "Please enter a valid URL.",
 6     date: "Please enter a valid date.",
 7     dateISO: "Please enter a valid date ( ISO ).",
 8     number: "Please enter a valid number.",
 9     digits: "Please enter only digits.",
10     equalTo: "Please enter the same value again.",
11     maxlength: $.validator.format( "Please enter no more than {0} characters." ),
12     minlength: $.validator.format( "Please enter at least {0} characters." ),
13     rangelength: $.validator.format( "Please enter a value between {0} and {1} characters long." ),
14     range: $.validator.format( "Please enter a value between {0} and {1}." ),
15     max: $.validator.format( "Please enter a value less than or equal to {0}." ),
16     min: $.validator.format( "Please enter a value greater than or equal to {0}." ),
17     step: $.validator.format( "Please enter a multiple of {0}." )
18   },


关注下面的标签,发现更多相似文章