JQuery Validate validation rules

Source: Internet
Author: User

Defining Chinese messages
var cnmsg = {
Required: "Required field",
Remote: "Please fix this field",
Email: "Please enter the correct format of e-mail",
URL: "Please enter a valid URL",
Date: "Please enter a valid date",
Dateiso: "Please enter a valid date (ISO).",
Number: "Please enter a valid digit",
Digits: "Can only enter integers",
CreditCard: "Please enter a valid credit card number",
Equalto: "Please enter the same value again",
Accept: "Please enter a string with a valid suffix name",
Maxlength:jQuery.format ("Please enter a string with a maximum length of {0}"),
Minlength:jQuery.format ("Please enter a string with a minimum length of {0}"),
Rangelength:jQuery.format ("Please enter a string between {0} and {1}"),
Range:jQuery.format ("Please enter a value between {0} and {1}"),
Max:jQuery.format ("Please enter a value of {0} max"),
Min:jQuery.format ("Please enter a value of minimum {0}")
};
Jquery.extend (JQuery.validator.messages, cnmsg);

Jquery Validate Validation rules

(1) Required:true must lose field
(2) Remote: "check.php" using Ajax method call check.php Validate input value
(3) Email:true must enter the correct format of e-mail
(4) Url:true must enter the correct format of the URL
(5) Date:true must enter the correct format date
(6) Dateiso:true must enter the correct format for the date (ISO), for example: 2009-06-23, 1998/01/22 only verify the format, do not verify the validity
(7) Number:true must enter a valid number (negative, fractional)
(8) Digits:true must enter an integer
(9) CreditCard: Must enter a valid credit card number
Equalto: The "#field" input value must be the same as #field
(one) Accept: Enter a string with a valid suffix (the suffix of the uploaded file)
Maxlength:5 input string with a maximum length of 5 (Chinese characters are counted as one character)
(minlength:10) Enter a string with a minimum length of 10 (Chinese characters are counted as one character)
(rangelength:[5,10] Enter a string that must be between 5 and 10 in length) (Chinese characters are counted as one character)
(range:[5,10] Input value must be between 5 and 10
(+) Max:5 input value cannot be greater than 5
(min:10) input value cannot be less than 10

Jquery Validate Custom validation rules

Addmethod (Name,method,message) method:
Parameter name is the name of the method added
The parameter method is a function that receives three parameters (Value,element,param) value is the values of the element, element is the elements themselves param
is a parameter, we can use Addmethod to add a validation method other than built-in Validation methods, such as having a field,
Can lose a letter, the range is a-f, the wording is as follows:

$.validator.addmethod ("AF", function (value,element,params) {
if (value.length>1) {
return false;
}
if (Value>=params[0]&&value<=params[1]) {
return true;
}else{
return false;
}
}, "must be a letter, and a-f");
Id= "username" in a form field, write in rules
username:{
Af:["A", "F"]
}
Method
The first parameter of the Addmethod is the name of the added validation method, which is the AF
The third parameter of Addmethod is the custom error prompt, where the prompt is: "must be a letter, and A-f"
The second parameter of the Addmethod is a function, which is more important and determines how to use this verification method.
If there is only one parameter, write directly, if AF: "A", then A is the only parameter, if multiple parameters, used in [], separated by commas

Jquery Validate Submit Submission
Submithandler: A function that runs after validation, which is added to the form-submitted function, or the form is not submitted
$ (". Selector"). Validate ({   submithandler:function (form) {$ (form). Ajaxsubmit ();          function with jquery form   }})

Jquery Validate error Bug tip Dom

. Errorplacement:callback Default: Put the error message behind the validated element
Indicates where the error is placed, by default: Error.appendto (Element.parent ()), where the error message is placed after the element being validated
Errorplacement:function (Error, Element) {
Error.appendto (Element.parent ());
}

Set the style of the error prompt, you can increase the icon display, Like:input.error {border:1px solid red;}
Label.error {
Background:url ("./demo/images/unchecked.gif") No-repeat 0px0px;  padding-left:16px;  padding-bottom:2px;  Font-weight:bold; Color: #EA5200;
} Appendix: Common Custom Validation rules

Character verification
JQuery.validator.addMethod ("Stringcheck", function (value, Element) {
return this.optional (Element) | | /^[u0391-uffe5w]+$/.test (value);
}, "can only include Chinese characters, English letters, numbers and underscores";

Chinese characters two bytes
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]);
}, "Make sure the value entered is between 3-15 bytes (2 bytes in one text)");

Identification Number Verification
JQuery.validator.addMethod ("Isidcardno", function (value, Element) {
return this.optional (Element) | | Isidcardno (value);
}, "Please enter your ID number correctly");

Mobile phone number Verification
JQuery.validator.addMethod ("IsMobile", function (value, Element) {
var length = Value.length;
var mobile =/^ ((13[0-9]{1}) | ( 15[0-9]{1})) +d{8}) $/;
return this.optional (Element) | | (length = = && Mobile.test (value));
}, "Please fill in your mobile phone number correctly");

Phone number Verification
JQuery.validator.addMethod ("Istel", function (value, Element) {
var tel =/^d{3,4}-?d{7,9}$/; Phone number format 010-12345678
return this.optional (Element) | | (Tel.test (value));
}, "Please fill in your phone number correctly");

Contact Phone (phone/Phone) verification
JQuery.validator.addMethod ("Isphone", function (value,element) {
var length = Value.length;
var mobile =/^ ((13[0-9]{1}) | ( 15[0-9]{1})) +d{8}) $/;
var tel =/^d{3,4}-?d{7,9}$/;
return this.optional (Element) | | (tel.test (value) | | mobile.test (value));

}, "Please fill in your contact number correctly");

ZIP Code Verification
JQuery.validator.addMethod ("Iszipcode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) | | (Tel.test (value));
}, "Please fill in your zip code correctly");


function Isidcardno (num) {

var Factorarr = NewArray (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
var paritybit=newarray ("1″," 0″, "X", "9″," 8″, "7″," 6″, "5″," 4″, "3″," 2″);
var vararray = new Array ();
Var intvalue;
var lngproduct = 0;
Var intcheckdigit;
var intstrlen = num.length;
var idNumber = num;
//Initialize
if ((intstrlen! =) && (intstrlen!=)) {
return false;
}
//check and set value
for (i=0;i<intstrlen;i++) {
Vararray[i] = Idnumber.charat (i);
if ((Vararray[i] < ' 0′| | vararray[i]> ' 9′) && (i! =)) {
return false;
} else if (I < +) {
VA Rarray[i] = vararray[i] * Factorarr[i];
}
}

if (Intstrlen = = 18) {
Check Date
var Date8 = idnumber.substring (6,14);
if (IsDate8 (date8) = = False) {
return false;
}
Calculate the sum of the products
for (i=0;i<17;i++) {
Lngproduct = lngproduct + vararray[i];
}
Calculate the check digit
Intcheckdigit = paritybit[lngproduct% 11];
Check last digit
if (vararray[17]! = intcheckdigit) {
return false;
}
}
else{//length is 15
Check Date
var date6 = idnumber.substring (6,12);
if (IsDate6 (date6) = = False) {

return false;
}
}
return true;

}

function IsDate6 (sdate) {
if (!/^[0-9]{6}$/.test (sdate)) {
return false;
}
Var year, month, day;
Year = sdate.substring (0, 4);
month = sdate.substring (4, 6);
if (Year < 1700 | | year > 2500) return False
if (Month < 1 | | month > Returnfalse)
return True
}

function IsDate8 (sdate) {
if (!/^[0-9]{8}$/.test (sdate)) {
return false;
}
Var year, month, day;
Year = sdate.substring (0, 4);
month = sdate.substring (4, 6);
Day = sdate.substring (6, 8);
var iamonthdays = [31,28,31,30,31,30,31,31,30,31,30,31]
if (Year < 1700 | | year > 2500) return False
if (((year% 4 = = 0) && (year% 100!= 0)) | | (Year% 400 = = 0)) iamonthdays[1]=29;
if (Month < 1 | | month > Returnfalse)
if (Day < 1 |, day >iamonthdays[month-1]) return False
return True
}
//Identification number verification
JQuery.validator.addMethod ("Idcardno", function (value, Element) {
return this.optional (Element) | | Isidcardno (value);
}, "Please enter your ID number correctly");

Alpha-Numeric
JQuery.validator.addMethod ("Alnum", function (value, Element) {
return this.optional (Element) | | /^[a-za-z0-9]+$/.test (value);
}, "can only include English letters and Numbers");

ZIP Code Verification
JQuery.validator.addMethod ("ZipCode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) | | (Tel.test (value));
}, "Please fill in the ZIP code correctly");

Chinese characters
JQuery.validator.addMethod ("Chcharacter", function (value, Element) {
var tel =/^[u4e00-u9fa5]+$/;
return this.optional (Element) | | (Tel.test (value));
}, "Please enter Chinese characters");

Minimum character length verification (one Chinese characters length is 2)
JQuery.validator.addMethod ("Stringminlength", 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);
}, $.validator.format ("length cannot be less than {0}!"));

Character Maximum length verification (one Chinese characters length is 2)
JQuery.validator.addMethod ("Stringmaxlength", 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);
}, $.validator.format ("length cannot be greater than {0}!"));

Character verification
JQuery.validator.addMethod ("string", function (value, Element) {
return this.optional (Element) | | /^[u0391-uffe5w]+$/.test (value);
}, "Do not allow special symbols!");

Mobile phone number Verification
JQuery.validator.addMethod ("mobile", function (value, Element) {
var length = Value.length;
return this.optional (Element) | | (Length = = 11&&/^ ((13[0-9]{1}) | ( 15[0-9]{1})) +d{8}) $/.test (value));
}, "Phone number format is wrong!");

Phone number Verification
JQuery.validator.addMethod ("Phone", function (value, Element) {
var tel =/^ (d{3,4}-?)? d{7,9}$/g;
return this.optional (Element) | | (Tel.test (value));
}, "Phone number format is wrong!");

ZIP Code Verification
JQuery.validator.addMethod ("ZipCode", function (value, Element) {
var tel =/^[0-9]{6}$/;
return this.optional (Element) | | (Tel.test (value));
}, "bad postal code format!");

Must be validated at the beginning of a specific string
JQuery.validator.addMethod ("Begin", function (value, element, param) {
var begin = New RegExp ("^" + param);
return this.optional (Element) | | (Begin.test (value));
}, $.validator.format ("must start with {0}!"));

Verify that the two-time input values are not the same
JQuery.validator.addMethod ("Notequalto", function (value, Element,param) {
return value! = $ (param). Val ();
}, $.validator.format ("two times input cannot be the same!"));

The validation value is not allowed with a specific value equal to
JQuery.validator.addMethod ("NotEqual", function (value, Element,param) {
return value! = param;
}, $.validator.format ("input value not allowed for {0}!"));

The validation value must be greater than a specific value (cannot be equal)
JQuery.validator.addMethod ("GT", function (value, element, param) {
return value > param;
}, $.validator.format ("Input value must be greater than {0}!"));

JQuery Validate validation rules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.