jquery's simplest form validation plug-in _jquery

Source: Internet
Author: User
Copy Code code as follows:

/*
Jquery Form Verification Plug-in
Janchie 2010.1 janchie@163.com
Version 1.01
*/
(function ($) {
$.fn.extend ({
Valid:function () {
if (! $ (this). are ("form") return;
Get parameters
var items = $.isarray (Arguments[0])? Arguments[0]: [],
Isbindsubmit = typeof Arguments[1] = = "Boolean"? ARGUMENTS[1]: True,
Isalert = typeof arguments[2] = = "Boolean"? ARGUMENTS[2]: false,
Validation 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]|[. _]) {4,19}$/,
"Password":/^ (\w) {6,20}$/,
"Safe":/>|<|,|\[|\]|\{|\}|\?| \/|\+|=|\|| \'|\\|\"|:|;| \~|\!| \@|\#|\*|\$|\%|\^|\&|\ (|\) | '/I,
"DBC":/[a-za-z0-9! @#¥%^&* () _+{}[]|: "';. ,/? <> ' ~]/,
"QQ":/[1-9][0-9]{4,}/,
"Date":/^ (((1[6-9]|[ 2-9]\d) \d{2})-(0?[ 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\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 Authentication hint information suffix
Msgsuffix = {
"Eng": "Only input English",
"CHN": "Only input Chinese characters",
"Mail": "Incorrect format",
"url": "Incorrect format",
"Currency": "Number format is wrong",
"Number": "Only for Numbers",
' int ': ' Only Integer ',
"Double": "Only digits with decimals",
"username": "Only for digital and English and underline and." Combination, beginning with the letter, 4-20 characters ",
"Password": "Only for numbers and the combination of English and underline, 6-20 characters",
"Safe": "Cannot have special characters",
"DBC": "Cannot have full-width characters",
"QQ": "The format is not correct",
"Date": "Incorrect format",
"Year": "Not correct",
"Month": "Not correct",
"Day": "Incorrect",
"Hour": "Not correct",
"Minute": "Not correct",
"Second": "Not correct",
"Mobile": "Incorrect format",
"Phone": "Wrong format",
"ZipCode": "Incorrect format",
"Bodycard": "Incorrect format",
"IP": "IP is incorrect",
' File ': ' Incorrect type ',
' Image ': ' Incorrect type ',
"word": "Incorrect type",
"EQ": "Not equal to",
"GT": "No greater than",
"GTE": "not greater than or equal to",
"LT": "no less than",
"LTE": "not less than or equal to"
},
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 prompted by the custom modification of the Obama
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);
},
Correct information tips for the custom modification of the Hu Jintao
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>");
},
Hint name that matches the contrast 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 to find;
},
Single-Element validation
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 + "no choice";
ShowError (field, I.name, warnmsg);
}else if (isrq && FV = "") {
Warnmsg = i.message| | I.simple + (field.is ("select")? "No choice": "Cannot be empty");
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 + "Length range should be between" +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.to? $.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.to. 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 Validation
Validate = function () {
$.each (items, function () {isall=true; Fieldcheck (this);});
if (Isalert && msg!= "") {
Alert (msg); msg = "";
}
return checkret;
};
Single-element event binding
$.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);
}
});
Commit Event Bindings
if (isbindsubmit) {
$ (this). submit (Validate);
}else{
return validate ();
}
}
});
}) (JQuery);

Parameters and Description:
-------------------Configuration parameters:----------------------
--------Form--------
Valid (Fileds,isbindsubmit,isalert)
Parameter one is an array of table items (JSON method) and must be
Parameter two is whether the form is a manual call validation result, the default is true, that is, to automatically verify the Submit event, optional;
Parameter three is an alert prompt for authentication information, and defaults to No, optional.
--------Table Item Array--------
Name: Name of the form field, required
Type: validation type, optional
Simple: Simply prompt the message, enter only the domain's Chinese name [recommended]
Message: Complete hint messages, instead of simple prompts
Require: Must fill, default is true, that is required, false is not required, optional
To: Match value contrast, object name, optional
Value: Direct matching value comparison, with to then the value is ignored, optional
Min: Minimum length, optional
Max: max length, optional
Ajax: An address to obtain the results of an asynchronous validation, optional
Type authentication types are as follows: Hu Jintao
ENG: English
CHN: Chinese Characters
Mail: Mailboxes
URLs: URLs
Currency: Currency
Number: Numbers
int: integer
Double: floating point numbers
Username: The combination of numbers and English and underline and., beginning with a letter, 4-20 characters
Password: combination of digital and English and underline, 6-20 characters
Safe: No special characters included
DBC: Full-angle characters (except Chinese characters)
Qq:5-9 bit number
Date: Time
Year: Years
Month: Month
Day: Days
Hour: Hour
minute: Min
Second seconds
Mobile: Cell phone
Phone: Telephone
ZipCode: Zip Code
Bodycard: ID card, support 15-digit, 18-digit, X-Letter
Ip:ip
File: Types of files
Image: Picture file type
Word: Document file type
The following types need to have a matching object or value * * *
EQ: =
GT: >
gte:>=
LT: <
lte:<=
How to be simple and easy method???
Look at the code:
Copy Code code as follows:

$ (function () {
$ ("form"). Valid ([
Validation selection
{Name: ' checkbox ', simple: ' Multiple selections '},
Verify required, as long as it is not empty
{Name: ' username ', simple: ' username '},
Non-required, mailbox type
{Name: "email", type: "Mail", Simple: "Mailbox", 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.