[Html]
Copy codeThe Code is as follows:
<Form action = "" method = "post" id = "formValidate">
Value: <input name = "" type = "text" validate = "number"/> <span> </span> <br/>
Floating Point: <input name = "" type = "text" validate = "decimal"/> <span> </span> <br/>
English: <input name = "" type = "text" validate = "english"/> <span> </span> <br/>
Uppercase English: <input name = "" type = "text" validate = "upper_english"/> <span> </span> <br/>
Lowercase English: <input name = "" type = "text" validate = "lower_english"/> <span> </span> <br/>
Email format: <input name = "" type = "text" validate = "email"/> <span> </span> <br/>
Include chinese characters: <input name = "" type = "text" validate = "chinese"/> <span> </span> <br/>
URL: <input name = "" type = "text" validate = "url"/> <span> </span> <br/>
Phone number: <input name = "" type = "text" validate = "phone"/> <span> </span> <br/>
Ip address: <input name = "" type = "text" validate = "ip"/> <span> </span> <br/>
Amount: <input name = "" type = "text" validate = "money"/> <span> </span> <br/>
The value is either an English or a _: <input name = "" type = "text" validate = "number_letter"/> <span> </span> <br/>
Zip code: <input name = "" type = "text" validate = "zip_code"/> <span> </span> <br/>
Available account: <input name = "" type = "text" validate = "account"/> <span> </span> <br/>
QQ: <input name = "" type = "text" validate = "qq"/> <span> </span> <br/>
ID card: <input name = "" type = "text" validate = "card"/> <span> </span> <br/>
Value can be blank: <input name = "" type = "text" validate = "number" empty = 'yes'/> <span> </span> <br/>
Value length: 1-3: <input name = "" type = "text" validate = "number" min = 1 max = 3/> <span> </span> <br/>
The length of a value ranging from 1 to 3 can be null: <input name = "" type = "text" validate = "number" min = 1 max = 3 empty = 'yes'/> <span> </span> <br/>
<Input name = "" type = "submit"/>
</Form>
<Script src = "jquery. js" type = "text/javascript"> </script>
<Script src = "formValidate. js" type = "text/javascript"> </script>
<Script type = "text/javascript">
Var formValidate = new formValidate ();
FormValidate. init ({});
</Script>
[Javascript]
Copy codeThe Code is as follows:
/*
* General JS verification class
* Usage:
* Var formValidate = new formValidate ();
* FormValidate. init ({});
* Note:
* <Form action = "" method = "post" id = "formValidate">
* The id is formValidate.
*
* <Input name = "" type = "text" validate = "zip_code" empty = "yes" min = 10 max = 10/> <span> </span>
* Validate = "zip_code" verify whether it is a zip code
* Empty = "yes" verify whether it is allowed to be empty
* Min = 10 min length
* Max = 10 max length
* <Span> </span> displays the prompt content.
*/
Var formValidate = function (){
Var _ this = this;
This. options = {
Number: {reg:/^ [0-9] + $/, str: 'must be a number '},
Decimal: {reg:/^ [-] {0, 1} (\ d +) [\.] + (\ d +) $/, str: 'must be in DECIMAL format '},
English: {reg:/^ [A-Za-z] + $/, str: 'must be an english letter '},
Upper_english: {reg:/^ [A-Z] + $/, str: 'must be uppercase letters '},
Lower_english: {reg:/^ [a-z] + $/, str: 'must be a lowercase English letter '},
Email: {reg:/^ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ | \.]?) * [A-zA-Z0-9] + \. [a-zA-Z] {2, 3} $/, str: 'incorrect Email format '},
Chinese: {reg:/[\ u4E00-\ u9FA5 \ uf900-\ ufa2d]/ig, str: 'must contain Chinese '},
Url: {reg:/^ [a-zA-z] +: \/[^ s] */, str: 'url format incorrect '},
Phone: {reg:/^ [1] [3] [0-9] {9 }$/, str: 'incorrect phone Number Format '},
Ip: {reg:/^ (\ d + )\. (\ d + )\. (\ d + )\. (\ d +) $/, str: 'IP address format incorrect '},
Money: {reg:/^ [0-9] + [\.] [0-9] {0, 3 }$/, str: 'amount format incorrect '},
Number_letter: {reg:/^ [0-9a-zA-Z \ _] + $/, str: 'Only English letters, numbers, _ '} are allowed ,_'},
Zip_code: {reg:/^ [a-zA-Z0-9] {3,12} $/, str: 'incorrect zip code format '},
Account: {reg:/^ [a-zA-Z] [a-zA-Z0-9 _] {} $/, str: 'The account name is invalid. It can contain 5 to 16 characters, UNDERLINES and digits '},
Qq: {reg:/[1-9] [0-9] {4,}/, str: 'incorrect QQ account '},
Card: {reg:/^ (\ d {6}) (18 | 19 | 20 )? (\ D {2}) ([01] \ d) ([0123] \ d) (\ d {3}) (\ d | X )? $/, Str: 'incorrect ID card number '},
};
// Initialize the binding form option
This. init = function (options ){
This. setOptions (options );
This. checkForm ();
};
// Set parameters
This. setOptions = function (options ){
For (var key in options ){
If (key in this. options ){
This. options [key] = options [key];
}
}
};
// Check whether the form contains null, maximum value and minimum value, and regular expression verification.
This. checkForm = function (){
$ ("# FormValidate"). submit (function (){
Var formChind = $ ("# formValidate"). children ();
Var testResult = true;
FormChind. each (function (I ){
Var child = formChind. eq (I );
Var value = child. val ();
Var len = value. length;
Var childSpan = child. next ();
// Whether the attribute is empty
If (child. attr ('empty ')){
If (child. attr ('empty') = 'yes' & value = ''){
If (childSpan ){
ChildSpan.html ('');
}
Return;
}
}
// Min and max maximum and minimum lengths in the attribute
Var min = null;
Var max = null;
If (child. attr ('Min') min = child. attr ('Min ');
If (child. attr ('max ') max = child. attr ('max ');
If (min & max ){
If (len <min | len> max ){
If (childSpan ){
ChildSpan.html ('');
ChildSpan.html ('string length between '+ min +' and '+ max + ');
TestResult = false;
Return;
}
}
} Else if (min ){
If (len <min ){
If (childSpan ){
ChildSpan.html ('');
ChildSpan.html ('string length should be greater than '+ min );
TestResult = false;
Return;
}
}
} Else if (max ){
If (len> max ){
If (childSpan ){
ChildSpan.html ('');
ChildSpan.html ('string length should be less than '+ max );
TestResult = false;
Return;
}
}
}
// Regular Expression Verification
If (child. attr ('validate ')){
Var type = child. attr ('validate ');
Var result = _ this. check (value, type );
If (childSpan ){
ChildSpan.html ('');
If (result! = True ){
ChildSpan.html (''+ result );
TestResult = false;
}
}
}
});
Return testResult;
});
};
// Detect a single Regular Expression
This. check = function (value, type ){
If (this. options [type]) {
Var val = this. options [type] ['reg '];
If (! Val. test (value )){
Return this. options [type] ['str'];
}
Return true;
} Else {
Return 'unable to find the Form Verification regularization ';
}
};
}