Copy codeThe Code is as follows:
/*
Jquery Form Verification plug-in
Janchie 2010.1 janchie@163.com
Version 1.01
*/
(Function ($ ){
$. Fn. extend ({
Valid: function (){
If (! $ (This). is ("form") return;
// Obtain parameters
Var items = $. isArray (arguments [0])? Arguments [0]: [],
IsBindSubmit = typeof arguments [1] = "boolean "? Arguments [1]: true,
IsAlert = typeof arguments [2] = "boolean "? Arguments [2]: false,
// Verification rules
Rule = {
// Regular rules
"Eng":/^ [A-Za-z] + $ /,
"Chn":/^ [\ u0391-\ uFFE5] + $ /,
"Mail":/\ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w + )*/,
"Url":/^ http [s]? : \ // [A-Za-z0-9] + \. [A-Za-z0-9] + [\/= \? % \-&_~ '@ [\] \': +!] * ([^ <> \ "\"]) * $ /,
"Currency":/^ \ d + (\. \ d + )? $ /,
"Number":/^ \ d + $ /,
"Int":/^ [0-9] {1, 30} $ /,
"Double":/^ [-\ +]? \ D + (\. \ d + )? $ /,
"Username":/^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {} $ /,
"Password":/^ (\ w) {6, 20} $ /,
"Safe":/>|<|,|\[ |\] |\{|\} | \? | \/| \ + | = | \ '| \ "|: |; | \~ | \! | \@ | \# | \* | \$ | \% | \^|\& | \ (|\) | '/I,
"Dbc":/[a-zA-Z0-9! @ # ¥ % ^ & * () _ + {} [] | :"';.,/? <> Region ~ ]/,
"Qq":/[1-9] [0-9] {4 ,}/,
"Date":/^ (1 [6-9] | [2-9] \ d) \ d {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ d) \ d {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ d) \ d {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ d) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $ /,
"Year":/^ (19 | 20) [0-9] {2} $ /,
"Month":/^ (0? [1-9] | 1 [0-2]) $ /,
"Day":/^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $ /,
"Hour":/^ (0? [1-9]) | (1 | 2) [0-3]) $ /,
"Minute":/^ (0? [1-9]) | (1 | 5) [0-9]) $ /,
"Second":/^ (0? [1-9]) | (1 | 5) [0-9]) $ /,
"Mobile":/^ (\ d {2, 3} \) | (\ d {3 }\-))? 13 \ d {9} $ /,
"Phone":/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /,
"Zipcode":/^ [1-9] \ d {5} $ /,
"Bodycard":/^ (1 [1-5]) | (2 [1-3]) | (3 [1-7]) | (4 [1-6]) | (5 [0-4]) | (6 [1-5]) | 71 | (8 [12]) | 91) \ d {4} (19 \ d {2} (0 [13-9] | 1 [012]) (0 [1-9] | [12] \ d | 30) | (19 \ d {2} (0 [13578] | 1 [02]) 31) | (19 \ d {2} 02 (0 [1-9] | 1 \ d | 2 [0-8]) | (19 ([13579] [26] | [2468] [048] | 0 [48]) 0229) \ d {3} (\ d | X | x )? $ /,
"Ip":/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) $ /,
"File":/^ [A-Za-z0-9] + (\. |-) [A-Za-z0-9] +) * \. [A-Za-z0-9] + $ /,
"Image": //. + \. (jpg | gif | png | bmp) $/I,
"Word":/. + \. (doc | rtf | pdf) $/I,
// Function rules
"Eq": function (arg1, arg2) {return arg1 = arg2? True: false ;},
"Gt": function (arg1, arg2) {return arg1> arg2? True: false ;},
"Gte": function (arg1, arg2) {return arg1> = arg2? True: false ;},
"Lt": function (arg1, arg2) {return arg1 <arg2? True: false ;},
"Lte": function (arg1, arg2) {return arg1 <= arg2? True: false ;}
},
// Simple verification prompt suffix
MsgSuffix = {
"Eng": "english only ",
"Chn": "Only Chinese characters can be entered ",
"Mail": "Incorrect format ",
"Url": "Incorrect format ",
"Currency": "The number format is incorrect ",
"Number": "numbers only ",
"Int": "can only be an integer ",
"Double": "Only digits with decimal digits ",
"Username": "A combination of numbers, English letters, underscores (_), and periods. It must start with a letter and contain 4-20 characters ",
"Password": "A combination of numbers, English letters, and underscores (_) is allowed ",
"Safe": "special characters are not allowed ",
"Dbc": "cannot have full-angle characters ",
"Qq": "Incorrect format ",
"Date": "Incorrect format ",
"Year": "Incorrect ",
"Month": "Incorrect ",
"Day": "Incorrect ",
"Hour": "Incorrect ",
"Minute": "Incorrect ",
"Second": "Incorrect ",
"Mobile": "Incorrect format ",
"Phone": "Incorrect format ",
"Zipcode": "Incorrect format ",
"Bodycard": "Incorrect format ",
"Ip": "Incorrect IP Address ",
"File": "incorrect type ",
"Image": "incorrect type ",
"Word": "incorrect type ",
"Eq": "not equal ",
"Gt": "Not greater ",
"Gte": "Not greater than or equal ",
"Lt": "not less ",
"Lte": "not less than or equal"
},
Msg = "", formObj = $ (this), checkRet = true, isAll,
Tipname = function (namestr) {return "tip _" + namestr. replace (/([a-zA-Z0-9])/g, "-$1 ");},
// Rule type matching detection
TypeTest = function (){
Var result = true, args = arguments;
If (rule. hasOwnProperty (args [0]) {
Var t = rule [args [0], v = args [1];
Result = args. length> 2? T. apply (arguments, []. slice. call (args, 1) :( $. isFunction (t )? T (v): t. test (v ));
}
Return result;
},
// Error message ******* custom modification ******
ShowError = function (fieldObj, filedName, warnInfo ){
CheckRet = false;
FieldObj.css ("background", "# FFDFDD ");
Var tipObj = $ ("#" + tipname (filedName ));
If (tipObj. length> 0) tipObj. remove ();
Var tipPosition = fieldObj. next (). length> 0? FieldObj. nextAll (). eq (this. length-1): fieldObj. eq (this. length-1 );
TipPosition. after ("<span style = 'color: # F06 'id = '" + tipname (filedName) + "'>" + warnInfo + "</span> ");
If (isAlert & isAll) msg + = "\ n" + warnInfo;
// If (isAlert &&! IsAll) alert (warnInfo );
},
// The correct information prompts ******* custom modification ******
ShowRight = function (fieldObj, filedName ){
FieldObj.css ("background", "# CCFFCC ");
Var tipObj = $ ("#" + tipname (filedName ));
If (tipObj. length> 0) tipObj. remove ();
Var tipPosition = fieldObj. next (). length> 0? FieldObj. nextAll (). eq (this. length-1): fieldObj. eq (this. length-1 );
TipPosition. after ("<span style = 'color: #0C0 'id = '" + tipname (filedName) + "'> correct </span> ");
},
// Prompt name matching the comparison value
FindTo = function (objName ){
Var find;
$. Each (items, function (){
If (this. name = objName & this. simple ){
Find = this. simple; return false;
}
});
If (! Find) find = $ ("[name = '" + objName + "']") [0]. name;
Return find;
},
// Single element Verification
FieldCheck = function (item ){
Var I = item, field = $ ("[name = '" + I. name + "']", formObj [0]);
If (! Field [0]) return;
Var warnMsg, fv = $. trim (field. val (), isRq = typeof I. require = "boolean "? I. require: true;
If (isRq & (field. is (": radio") | field. is (": checkbox "))&&! Field. is (": checked "))){
WarnMsg = I. message | I. simple + "not selected ";
ShowError (field, I. name, warnMsg );
} Else if (isRq & fv = ""){
WarnMsg = I. message | I. simple + (field. is ("select ")? "Not selected": "cannot be blank ");
ShowError (field, I. name, warnMsg );
} Else if (fv! = ""){
If (I. min | I. max ){
Var len = fv. length, min = I. min | 0, max = I. max;
WarnMsg = I. message | (max? I. simple + "the length range should be" + min + "~ "+ Max +": I. simple + "length should be greater than" + min );
If (max & (len> max | len <min) | (! Max & len <min )){
ShowError (field, I. name, warnMsg); return;
}
}
If (I. type ){
Var matchVal = I.? $. Trim ($ ("[name = '" + I. to + "']"). val (): I. value;
Var matchRet = matchVal? TypeTest (I. type, fv, matchVal): typeTest (I. type, fv );
WarnMsg = I. message | I. simple + msgSuffix [I. type];
If (matchVal & I. simple) warnMsg + = (I.? FindTo (I. to) + "value": I. value );
If (! MatchRet) showError (field, I. name, warnMsg );
Else showRight (field, I. name );
} Else {
ShowRight (field, I. name );
}
} Else if (isRq ){
ShowRight (field, I. name );
}
},
// Element group verification
Validate = function (){
$. Each (items, function () {isAll = true; fieldCheck (this );});
If (isAlert & msg! = ""){
Alert (msg); msg = "";
}
Return checkRet;
};
// Bind a single-element event
$. Each (items, function (){
Var field = $ ("[name = '" + this. name + "']", formObj [0]);
If (field. is (": hidden") return;
Var obj = this, toCheck = function () {isAll = false; fieldCheck (obj );};
If (field. is (": file") | field. is ("select ")){
Field. change (toCheck );
} Else {
Field. blur (toCheck );
}
});
// Submit event binding
If (isBindSubmit ){
$ (This). submit (validate );
} Else {
Return validate ();
}
}
});
}) (JQuery );
Parameters and descriptions:
------------------- Configuration parameter :----------------------
-------- Form --------
Valid (fileds, isBindSubmit, isAlert)
Parameter 1 is an array of form items (in Json format), required
Parameter 2 indicates whether the form manually calls the verification result. The default value is true, that is, the submit event is automatically verified. Optional;
Parameter 3 indicates that the authentication information is prompted by Alert. The default value is no. Optional.
-------- Array of Form Items --------
Name: name of the form field, required
Type: verification type. Optional.
Simple: a simple message. Only the Chinese name of the domain is entered. [recommended]
Message: complete prompt message instead of simple prompt message
Require: required or not. The default value is true, which is required. false is optional.
To: matching Value Comparison, Object name, optional
Value: directly matches the value Comparison. If yes, the value is ignored. Optional.
Min: minimum length. Optional.
Max: Maximum length. Optional.
Ajax: the address for obtaining the asynchronous verification result. Optional.
* ***** Type verification type :******
Eng: English
Chn: Chinese Characters
Mail: Email
Url: url
Currency: currency
Number: number
Int: integer
Double: Floating Point Number
Username: A combination of numbers, English letters, underscores, and periods. It must start with a letter and contain 4-20 characters.
Password: A combination of numbers, English letters, and underscores (_). It must be 6-20 characters long.
Safe: does not contain special characters
Dbc: contains full-width characters (excluding Chinese characters)
Qq: 5-9 digits
Date: Time
Year: year
Month: month
Day
Hour: hour
Minute: minute
Second seconds
Mobile: mobile Phone
Phone: phone number
Zipcode: Zip code
Bodycard: ID card, which can contain 15 characters, 18 characters, and x letters
Ip: IP
File: file type
Image: image file type
Word: document file type
* ** The following types require matching objects or values ****
Eq: =
Gt:>
Gte:> =
Lt: <
Lte: <=
How can it be simple ???
Check the Code:
Copy codeThe Code is as follows:
$ (Function (){
$ ("Form"). valid ([
// Verify the selection
{Name: "checkbox", simple: "multiple choice "},
// Verification required, as long as it is not empty
{Name: "username", simple: "username "},
// Optional; email type
{Name: "email", type: "mail", simple: "email", require: false}
]);
})
DEMO code package download http://xiazai.jb51.net/201002/yuanma/jquery_checkform.rar