Custom Jquery.validate.js Verification Method __js

Source: Internet
Author: User

Blog reference:

Jquery.validate.js is a front-end form form check plug-in, specific details can be Baidu.

Jquery.validate.js not only contains some commonly used front-end checksums, but also provides convenient custom verification methods for expansion.

front-end built-in self-tape check:

Required:true Required Fields
Remote: "Check.php" uses Ajax methods to invoke check.php validate input values
Email:true input must be in the correct format for e-mail
Url:true input must be in the correct format URL
Date:true the date the input must be in the correct format
Verify format only, not validate validity
Number:true input must be a valid number (negative, decimal)
Digits:true input must be an integer
CreditCard: The entry must be a legal credit card number
Equalto: "#field" input value must be the same as #field
Accept: "Gif|png|jpg" Enter a string with the legal suffix name (the suffix of the uploaded file), between multiple suffixes with ' | ' Separated
Maxlength:5 enter a 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 (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

Custom Checksum method:

Common Regular Check example: only allowed to enter Chinese or letters

$.validator.addmethod ("Chineseandletter", function (value, Element) {
		var score =/^[a-za-z\u4e00-\u9fa5]+$/;
		return this.optional (Element) | | (Score.test (value);
	}, "Please enter Chinese or letter");

Set parameters for auxiliary checksums based on script invocation validation method Example: Verify the length of the input content (two bytes in Chinese) single parameter

JQuery.validator.addMethod ("ByteLength", function (value, Element,param) {
		var length = "";
		var Regu = "^[a-za-z\u4e00-\u9fa5]+$";
		var re = new RegExp (regu);
		if ( (re)!=-1) {
			length = Value.replace (/[\u0391-\uffe5]/g, "AA"). length;
		return this.optional (Element) | | (length >= param);     The arguments passed in Parm:js when the method is called, for example: Bytelength:4  
       $.validator.format ("Please enter at least {0} bytes (2 bytes in Chinese)");

Front-end script passes in multiple parameters

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]); The front-end script passes in multiple parameters, Param[0] takes the first incoming parameter;

PARM[1] takes the second argument passed in

Front-end Script Call modification method: Byterangelength: [2,10]

$.validator.format ("Make sure the value entered is between {0}-{1} bytes (2 bytes in one)");

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: 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.