Once posted on the classic Forum, this is used in personal projects during background processing, which is quite convenient for simple form verification.
Because you don't want to make the code too bloated, you don't need to add more features that are not commonly used.
I have not made any changes to my views. Why? Because I am lazy, haha
Today, we can see that omeweb has also modified a version and made many modifications, which is quite good. Thank you.
The source code is here:
// Remove spaces on both sides of the string
String. prototype. trim = function (){
Return this. replace (/(^ \ s +) | (\ s + $)/g ,"");
}
// Check whether the string is empty
String. prototype. isEmpty = function (){
Return! (/.? [^ \ S] +/. test (this ));
}
// Check whether the value is between two specified values
String. prototype. isBetween = function (val, min, max ){
Return isNaN (val) = false & val> = min & val <= max;
}
// Obtain the maximum or minimum value
String. prototype. getBetweenVal = function (what ){
Var val = this. split (',');
Var min = val [0];
Var max = val [1] = null? Val [0]: val [1];
If (parseInt (min)> parseInt (max )){
Min = max;
Max = val [0];
}
Return what = 'Min '? (IsNaN (min )? Null: min): (isNaN (max )? Null: max );
}
Var validator = function (formObj ){
This. allTags = formObj. getElementsByTagName ('*');
// Regular Expression for string Verification
This. reg = new Object ();
This. reg. english =/^ [a-zA-Z0-9 _ \-] + $ /;
This. reg. chinese =/^ [\ u0391-\ uFFE5] + $ /;
This. reg. number =/^ [-\ +]? \ D + (\. \ d + )? $ /;
This. reg. integer =/^ [-\ +]? \ D + $ /;
This. reg. float =/^ [-\ +]? \ D + (\. \ d + )? $ /;
This. reg. date =/^ (\ d {4}) (-| \/) (\ d {1, 2}) \ 2 (\ d {1, 2}) $ /;
This. reg. email =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $ /;
This. reg. url =/^ (ht | f) tp (s ?)) \: \ //) [A-zA-Z0-9] + \. [a-zA-Z0-9] + [\/= \? % \-&_~ '@ [\]
\ ': +!] * ([^ <> \ "\"]) * $ /;
This. reg. phone =/^ (\ d {2, 3} \) | (\ d {3 }\-))? (\ (0 \ d {2, 3} \) | 0 \ d {2, 3 }-)? [1-9] \ d {6, 7} (\-\ d
{1, 4 })? $ /;
This. reg. mobile =/^ (\ d {2, 3} \) | (\ d {3 }\-))? (13 \ d {9}) | (159 \ d {8}) $ /;
This. reg. ip =/^ (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4] \ d | 25 [0-5]). (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4]
\ D | 25 [0-5]). (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4] \ d | 25 [0-5]). (0 | [1-9] \ d? | [0-1] \ d {2} | 2 [0-4] \ d | 25 [0-4-
5]) $ /;
This.reg.zip code =/^ [1-9] \ d {5} $ /;
This. reg. qq =/^ [1-9] \ d {4, 10} $ /;
This. reg. msn =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $ /;
This. reg. idcard =/(^ \ d {15} $) | (^ \ d {17} [0-9Xx] $ )/;
// Error output
This. tip = new Object ();
This. tip. unknow = 'unfound verification type, unable to perform verification. ';
This. tip. paramError = 'the parameter is set incorrectly and cannot be verified. ';
This. tip. required = 'cannot be blank. ';
This. tip. english = 'Only english characters and underlines (a-zA-Z0-9 _) are allowed _). ';
This. tip. chinese = 'only chinese characters are allowed. ';
This. tip. number = 'is not a valid number. ';
This. tip. integer = 'is not a valid integer. ';
This. tip. float = 'is not a valid floating point number. ';
This. tip. date = 'is not a valid date format. (Example )';
This. tip. email = 'is not a valid email format. ';
This. tip. url = 'is not a valid hyperlink format. ';
This. tip. phone = 'is not a valid phone number. ';
This. tip. mobile = 'is not a valid mobile phone number. ';
This. tip. ip = 'is not a valid ip address. ';
This.tip.zip code = 'is not a valid zip code. ';
This. tip. qq = 'is not a valid qq number. ';
This. tip. msn = 'is not a valid MSN account. ';
This. tip. idcard = 'is not a valid ID card number. ';
// Obtain the control name
This. getControlName = function ()
{
Return this. element. getAttribute ('controlname') = null
? 'Specifies the control value'
: This. element. getAttribute ('controlname ');
}
// Set the focus
This. setFocus = function (ele ){
Try {
Ele. focus ();
} Catch (e ){}
}
// Set the border color
This. setBorderColor = function (ele ){
Var borderColor = ele. currentStyle?
Ele. currentStyle. borderColor:
Document. defaultView. getComputedStyle (ele, null) ['bordercolor'];
Ele. style. borderColor = '# ff9900 ';
Ele. onkeyup = function (){
This. style. borderColor = borderColor;
}
}
// Output Error feedback
This. feedback = function (type ){
Try {
Var msg = eval ('this. tip. '+ type) = undefined?
Type:
This. getControlName () + eval ('this. tip. '+ type );
} Catch (e ){
Msg = type;
}
This. setBorderColor (this. element );
Alert (msg );
This. setFocus (this. element );
};
// Perform string Verification
This. validate = function (){
Var v = this. element. value;
// Verify whether non-null values are allowed
Var required = this. element. getAttribute ('required ');
If (required! = Null & v. isEmpty ()){
This. feedback ('required ');
Return false;
}
// Verify whether the format is valid
Var dataType = this. element. getAttribute ('ype ype ');
If (! V. isEmpty () & dataType! = Null & dataType. toLowerCase ()! = 'Password '){
DataType = dataType. toLowerCase ();
Try {
If (! (Eval ('this. reg. '+ dataType). test (v )){
This. feedback (dataType );
Return false;
}
} Catch (e ){
This. feedback ('unknow ');
Return false;
}
}
// Perform data verification
Var confirm = this. element. getAttribute ('Confirm ');
If (confirm! = Null ){
Try {
Var data = eval ('formobj. '+ confirm +'. value ');
If (v! = Data ){
Alert ('the two inputs are inconsistent. Please enter them again. ');
This. setBorderColor (this. element );
This. setFocus (this. element );
Return false;
}
} Catch (e ){
This. feedback ('paramerror ');
Return false;
}
}
// Verify the numeric size
Var dataBetween = this. element. getAttribute ('databetween ');
If (! V. isEmpty () & dataBetween! = Null ){
Var min = dataBetween. getBetweenVal ('Min ');
Var max = dataBetween. getBetweenVal ('max ');
If (min = null | max = null ){
This. feedback ('paramerror ');
Return false;
}
If (! V. isBetween (v. trim (), min, max )){
This. feedback (this. getControlName () + 'must be between' + min + '-' + max +'
. ');
Return false;
}
}
// Verification character Length
Var dataLength = this. element. getAttribute ('datalength ');
If (! V. isEmpty () & dataLength! = Null ){
Var min = dataLength. getBetweenVal ('Min ');
Var max = dataLength. getBetweenVal ('max ');
If (min = null | max = null ){
This. feedback ('paramerror ');
Return false;
}
If (! V. isBetween (v. trim (). length, min, max )){
This. feedback (this. getControlName () + 'must be' + min + '-' + max + 'characters
. ');
Return false;
}
}
Return true;
};
// Perform Initialization
This. init = function (){
For (var I = 0; I <this. allTags. length; I ++ ){
If (this. allTags [I]. tagName. toUpperCase () = 'input' |
This. allTags [I]. tagName. toUpperCase () = 'select' |
This. allTags [I]. tagName. toUpperCase () = 'textea ')
{
This. element = allTags [I];
If (! This. validate ())
Return false;
}
}
};
Return this. init ();
}