Jquery's simplest and easiest form verification plug-in

Source: Internet
Author: User

CopyCode The 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 )? $ /,
"Ip":/^ (\ D {1, 2} | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ D {1, 2} | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ D {1, 2} | 1 \ D \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ D {1, 2} | 1 \ D \ 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 code The 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

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.