Super powerful Form Verification Form Verification class Validator v1.01
Script
/*************************************** **********
Validator v1.01
Code by my Buddha
Wfsr@cunite.com
Http://www.cunite.com
**************************************** *********/
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 + $ /,
Double:/^ [-\ +]? \ D + (\. \ d + )? $ /,
English:/^ [A-Za-z] + $ /,
Chinese:/^ [\ u0391-\ uFFE5] + $ /,
UnSafe:/^ ([A-Z] * | [a-z] * | \ d * | [-_\~! @ # \ $ % \ ^ & \ * \. \ (\) \ [\] \ {\} <> \? \/\ '\ "] *) |. {0, 5}) $ | \ s /,
IsSafe: function (str) {return! This. UnSafe. test (str );},
SafeString: "this. IsSafe (value )",
Limit: "this. limit (value. length, getAttribute ('Min'), getAttribute ('max '))",
LimitB: "this. limit (this. LenB (value), getAttribute ('Min'), getAttribute ('max '))",
Date: "this. IsDate (value, getAttribute ('Min'), getAttribute ('format '))",
Repeat: "value = document. getElementsByName (getAttribute ('to') [0]. value ",
Range: "getAttribute ('Min') <value & value <getAttribute ('max ')",
Compare: "this. compare (value, getAttribute ('operator'), getAttribute ('to '))",
Custom: "this. Exec (value, getAttribute ('regexp '))",
Group: "this. MustChecked (getAttribute ('name'), getAttribute ('Min'), getAttribute ('max '))",
ErrorItem: [document. forms [0],
ErrorMessage: ["failed submission due to the following reasons: \ 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 With (obj. elements [I]) {
Var _ dataType = getAttribute ("dataType ");
If (typeof (_ dataType) = "object" | typeof (this [_ dataType]) = "undefined") continue;
This. ClearState (obj. elements [I]);
If (getAttribute ("require") = "false" & value = "") continue;
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 (value )){
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 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 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 true;
},
Limit: function (len, min, max ){
Min = min | 0;
Max = max | Number. MAX_VALUE;
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 new RegExp (reg, "g"). test (op );
},
Compare: function (op1, operator, op2 ){
Switch (operator ){
Case "NotEqual ":
Return (op1! = Op2 );
Case "GreaterThan ":
Return (op1> op2 );
Case "GreaterThanEqual ":
Return (op1> = op2 );
Case "LessThan ":
Return (op1 <op2 );
Case "LessThanEqual ":
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 ("^ (\ d {4}) | (\ d {2 }))([-. /]) (\ d {1, 2}) \ 4 (\ d {1, 2}) $ "));
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 ("^ (\ d {1, 2 })([-. /]) (\ d {1, 2}) \ 2 (\ d {4}) | (\ d {2}) $ "));
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;
}
If (! ParseInt (month) return false;
Month = 12? 0: month;
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? "20": "19") + y) | 0 ;}
}
}
Script