注意/如果参数有多个可以使用数组,如regex:[1">
Home > Web Front-end > JS Tutorial > JQuery extension plug-in Validate 5 adds custom validation method_jquery

JQuery extension plug-in Validate 5 adds custom validation method_jquery

WBOY
Release: 2016-05-16 18:02:46
Original
992 people have browsed it

An example is as follows:

Copy code The code is as follows:



Run result:

Note that if there are multiple parameters, you can use an array, such as regex:[1,3,5 ], the method can be accessed using subscripts: params[0]
Some extended verification methods provided by netizens:
Copy code The code is as follows:

//Mobile phone number verification
jQuery.validator.addMethod("mobile", function(value, element) {
var length = value.length;
var mobile = /^(((13[0-9]{1})|(15[0-9]{1})) d{8})$/
return this.optional(element) | | (length == 11 && mobile.test(value));
}, "Mobile phone number format is wrong");
// Phone number verification
jQuery.validator.addMethod("phone", function (value, element) {
var tel = /^(0[0-9]{2,3}-)?([2-9][0-9]{6,7}) (-[0 -9]{1,4})?$/;
return this.optional(element) || (tel.test(value));
}, "Telephone number format error");
// Zip code validation
jQuery.validator.addMethod("zipCode", function(value, element) {
var tel = /^[0-9]{6}$/;
return this. optional(element) || (tel.test(value));
}, "Postal code format error");
// QQ number verification
jQuery.validator.addMethod("qq", function (value, element) {
var tel = /^[1-9]d{4,9}$/;
return this.optional(element) || (tel.test(value));
}, "QQ number format error");
//IP address verification
jQuery.validator.addMethod("ip", function(value, element) {
var ip = /^(? :(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0 -5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return this.optional(element) || (ip. test(value) && (RegExp.$1 < 256 && RegExp.$2 < 256 && RegExp.$3 < 256 && RegExp.$4 < 256));
}, "Ip address format is wrong");
// Validation of letters and numbers
jQuery.validator.addMethod("chrnum", function(value, element) {
var chrnum = /^([a-zA-Z0-9] )$/ ;
return this.optional(element) || (chrnum.test(value));
}, "Only numbers and letters (characters A-Z, a-z, 0-9) can be entered");
// Chinese validation
jQuery.validator.addMethod("chinese", function(value, element) {
var chinese = /^[u4e00-u9fa5] $/;
return this.optional(element) ) || (chinese.test(value));
}, "Only Chinese can be entered");
// Drop-down box verification
$.validator.addMethod("selectNone", function(value, element) {
return value == "Please select";
}, "One must be selected");
// Byte length verification
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
var length = value.length;
for (var i = 0; i < value.length; i ) {
if (value.charCodeAt(i) > 127) {
length ;
}
}
return this.optional(element) || (length >= param[0] && length <= param[1]);
}, $.validator.format("Please ensure that the entered value is between {0}-{1} bytes (one Chinese character counts as 2 bytes)"));

Validate_20110905.rar
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template