Javascript validation Validator.js Use full solution (reprint) __ Large data

Source: Internet
Author: User

file Validator.js

validator={
Require:/.+/,
Email:/^\w+ ([-+.] \w+) *@\w+ ([-.] \\w+) *\.\w+ ([-.] \w+) *$/,
Phone:/^ ((\d{3}\) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7}$/,
Mobile:/^ ((\d{3}\) | ( \d{3}\-))? 13\d{9}$/,
URL:/^http:\/\/[a-za-z0-9]+\. [a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "\"]) *$/,
Idcard:/^\d{15} (\d{2}[a-za-z0-9])? $/,
Currency:/^\d+ (\.\d+)? $/,
Number:/^\d+$/,
Zip:/^[1-9]\d{5}$/,
QQ:/^[1-9]\d{4,8}$/,
Integer:/^[-\+]?\d+$/,
Do le:/^[-\+]?\d+ (\.\d+) $/,
中文版:/^[a-za-z]+$/,
Chinese:/^[\α-\¥]+$/,
UnSafe:/^ ([a-z]*|[ a-z]*|\d*| [-_\~!@#\$%\^&\*\.\ (\) \[\]\{\}<>\?\\\/\ ' \ "]*) |. {0,5}) $|\s/,
Issafe:function (str) {return!this. Unsafe.test (str);},
Safestring: "This. Issafe (val) ",
Limit: "This.limit (Val.length,getattribute (' min '), getattribute (' Max ')",
LIMITB: "This.limit" (this. LenB (Val), getattribute (' min '), getattribute (' Max ') ",
Date: "This. IsDate (Val, getattribute (' min '), getattribute (' format ')) ",
Repeat: "val = = Document.getelementsbyname (getattribute (' to ')") [0].val]
Range: "GetAttribute (' min ') < Val && Val < getattribute (' Max ')",
Compare: "This.compare (Val,getattribute (' operator '), getattribute (' to ')"),
Custom: "This." Exec (Val, getattribute (' regexp ')) ",
Group: "This. Mustchecked (getattribute (' name '), getattribute (' min '), getattribute (' Max ') ",
Erroritem: [Document.forms[0]],
ErrorMessage: ["The cause of failure as below: \t\t\t\t"],
Validate:function (theform, mode) {
var obj = theform | | Event.srcelement;
var count = obj.elements.length;
This. Errormessage.length = 1;
This. Erroritem.length = 1;
This. Erroritem[0] = obj;
for (Var i=0;i<count;i++) {
With (Obj.elements[i]) {
var _datatype = getattribute ("DataType";
if (typeof (_datatype) = = "Object" | | | typeof (This[_datatype]) = = "undefined" contin;
This. Clearstate (Obj.elements[i]);
if (GetAttribute ("Require" = "false" && val = = "" Contin;
Switch (_datatype) {
Case "Date":
Case "Repeat":
Case "Range":
Case "Compare":
Case "Custom":
Case "Group":
Case "Limit":
Case "LIMITB":
Case "safestring":
if (!eval (This[_datatype])) {
This. Adderror (i, GetAttribute ("msg");
}
Break
Default:
if (!this[_datatype].test (val)) {
This. Adderror (i, GetAttribute ("msg");
}
Break
}
}
}
if (this. Errormessage.length > 1) {
mode = Mode | | 1;
var ErrCount = this. Erroritem.length;
Switch (mode) {
Case 2:
for (Var i=1;i<errcount;i++)
This. Erroritem[i].style.color = "Red";
Case 1:
Alert (this. Errormessage.join ("\ n");
This. Erroritem[1].focus ();
Break
Case 3:
for (Var i=1;i<errcount;i++) {
try{
var span = document.createelement ("span";
Span.id = "__errormessagepanel";
Span.style.color = "Red";
This. Erroritem[i].parentnode.appendchild (span);
Span.innerhtml = this. Errormessage[i].replace (/\d+:/, "*";
}
catch (E) {alert (e.description);}
}
This. Erroritem[1].focus ();
Break
Default:
Alert (this. Errormessage.join ("\ n");
Break
}
return false;
}
return TR;
},
Limit:function (Len,min, max) {
min = Min | | 0;
max = Max | | Number.max_val;
return min <= len && len <= Max;
},
Lenb:function (str) {
Return Str.replace (/[^\x00-\xff]/g, "* *". Length;
},
Clearstate:function (elem) {
With (Elem) {
if (Style.color = = "Red"
Style.color = "";
var lastnode = parentnode.childnodes[parentnode.childnodes.length-1];
if (lastnode.id = = "__errormessagepanel"
Parentnode.removechild (Lastnode);
}
},
Adderror:function (index, str) {
This. Erroritem[this. Erroritem.length] = this. Erroritem[0].elements[index];
This. Errormessage[this. Errormessage.length] = this. Errormessage.length + ":" + str;
},
Exec:function (OP, Reg) {
Return to New RegExp (Reg, "G". Test (OP);
},
Compare:function (OP1,OPERATOR,OP2) {
Switch (operator) {
Case "Noteq L":
Return (OP1!= OP2);
Case "GreaterThan":
Return (Op1 > OP2);
Case "Greaterthaneq L":
Return (OP1 >= OP2);
Case "LessThan":
Return (OP1 < OP2);
Case "Lessthaneq L":
Return (OP1 <= OP2);
Default
return (OP1 = = OP2);
}
},
Mustchecked:function (name, Min, max) {
var groups = Document.getelementsbyname (name);
var haschecked = 0;
min = Min | | 1;
max = Max | | Groups.length;
for (Var i=groups.length-1;i>=0;i--)
if (groups[i].checked) haschecked++;
return min <= haschecked && haschecked <= Max;
},
Isdate:function (OP, formatstring) {
formatstring = formatstring | | "Ymd";
var m, year, month, day;
Switch (formatstring) {
Case "YMD":
m = Op.match (New RegExp ("^\\s*" (\\d{4}) | ( \\D{2})) ([-./]) (\\d{1,2}) \\4 (\\d{1,2}) \\s*$ ");
if (M = = null) return false;
Day = m[6];
month = m[5]--;
Year = (M[2].length = 4)? M[2]: getFullYear (parseint (m[3), 10));
Break
Case "DMY":
m = Op.match (New RegExp ("^\\s* (\\d{1,2}) ([-./]) (\\d{1,2}) \\2 ((\\d{4}) | ( \\D{2})); \\s*$ ");
if (M = = null) return false;
Day = m[1];
month = m[3]--;
Year = (M[5].length = 4)? M[5]: getFullYear (parseint (m[6), 10));
Break
Default:
Break
}
var date = new Date (year, month, day);
return (typeof date = = "Object" && year = = Date.getfullyear () && month = = Date.getmonth () && Day = = Date.getdate ());
function getFullYear (y) {return (y<30?) "A": "+ y" |
}
}


JS Code

Use:

Grammar :dataType= "Require | Chinese | 中文版 | number | Integer | Do Le | Email | URL | Phone | Mobile | Currency | Zip | Idcard | QQ | Date | safestring | Repeat | Compare | Range | Limit | LIMITB | Group | Custom "
type : string . Must be selected.
description : Used to set the type of input data validation for a table item.
Optional Value Description :
Optional value Validation features
Require Required Fields
Chinese Chinese
中文版 English

Number
Digital
Integer
Integer
Do Le
Real
Email
Email address format
Url
URL format based on HTTP protocol
Phone
Phone number format
Mobile
Mobile phone number format
Currency Currency format
Zip
Zip code
Idcard
ID number
Qq
QQ number
Date Date
Safestring Secure Password
Repeat
Repeat input
Compare
Relationship comparison
Range
Input range
Limit
Limit input Length
Limitb
Limit the length of bytes entered
Group Verify single/Multiple Select button groups
Custom Custom Regular Expression validation
syntax : max = "int"
type : String. Optional when the DataType property value is Range , which is group and when the item is a multiple-selection button group (the default is 1 ), optional for LIMIT/LIMITB (at which time the default is 1.7976931348623157e+308 , that is, number . The value of Max_val .
Description : When the Datype property value is Range , use to determine if the entry is in the mi n between the property values of Max , when the DataType property value is group , and when the item is a multiple-select button Group, Use to set the number of selected button groups to determine whether the selected number is in the [ min ,   Max ] interval, when the Datype property value is Limit , used to verify that the number of characters entered is in the [ min ,   Max ] interval, when datype The property value is LIMITB when the number of bytes used to validate the input character is in the [ min ,   Max ] interval.
Syntax : min = "int"
type : String. Optional when the DataType property value is Range , as group and when the item is a multiple-selection button group (the default value is 1); LIMIT/LIMITB is optional (at which point the default value is 0).
Description : When the Datype property value is Range , use to determine if the entry is in the mi n between the property values of Max , when the DataType property value is group , and when the item is a multiple-select button Group, Use to set the number of selected button groups to determine whether the selected number is in the [ min ,   Max ] interval, when the Datype property value is Limit , used to verify that the number of characters entered is in the [ min ,   Max ] interval, when datype The property value is LIMITB when the number of bytes used to validate the input character is in the [ min ,   Max ] interval.
syntax : msg= "string"
type : String. Must be selected.
Description : The error message to be prompted when validation fails.
Grammar :operator= "Noteq L | GreaterThan | Greaterthaneq L | LessThan | Lessthaneq L | Eq L "
type : String. Optionally (the default value is EQ L) when the DataType property value is Compare .
Description : Reference to property.
The relational operators for the selected values :
, meaning ,
optional
noteq l
not equal to!=
GREATERTHAN&NBSP greater than
greaterthaneq l
greater than or equal > =
lessthan  less than:
lessthaneq L&NBSP is less than or equal <=
Eq l equals =
syntax :require= "TR | False
type : String. Optional.
description : Used to set the authentication method for a table item. The form item is not required when the value is false, but when it is filled in, the validation method that is set by the DataType property is still executed, and can be omitted when the values are TR or any non-false characters.
syntax :to = "Sting | int
type : String. Must be selected when the dataType value is Repeat or Compare .
Note : When dataType value is Repeat , the value of To is the name property value of a form item. Determines whether the value of the current form item is consistent with the value of the target table item, and when the value of the dataType is Compare , to the selected value type is real, which is used to determine the input of the current form item and to conforms to the relationship specified by the value of the operator property.
syntax :format= "Ymd | Dmy "
type : String. Optional when the DataType property value is Date (the default value is Ymd).
description : Used to verify that the input is a date that conforms to the format specified by the Format property value.
Conforming to the rule input example: 2004-11-23,2004/11/23,04.11.23,23-11-2004 , etc.
Note : When the year entered is 2 digits, if the value is less than 30, the year is considered to be in 21st century, otherwise it will be 20th century.
syntax :regexp= "Object"
type : String. Must be selected when the DataType property value is custom.
description : Used to verify that the input conforms to the regular expression specified by the regexp property.
Related Article

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.