Home >Web Front-end >JS Tutorial >jquery.validate usage guide Step 5 Regular verification_jquery
//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 error");
// 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));
}, "Phone number format error");
// Postal code verification
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 }, "Ip address format error");
// 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";
}, "Must select one");
// Byte length verification
jQuery.validator .addMethod("byteRangeLength", function(value, element, param) {
var length = value.length;
for (var i = 0; i if ( value.charCodeAt(i) > 127) {
length ;
}
}
return this.optional(element) || (length >= param[0] && length }, $.validator.format("Please ensure that the entered value is between {0}-{1} bytes (one Chinese character counts as 2 bytes)"));