Form Validation////////////////////////////////////////////////////////////////////////////////
/*
*---------------Common validation checkform (oform) for client forms-----------------
* Function: Universal validation of all form elements.
Use
* <form name= "Form1" >
* <input type= "name=" id "check=" ^\s+$ "warning=" ID cannot be empty and cannot contain spaces ">
* <input type= "Submit" >
* </form>
* Implemented features:
* Verify the legality of the Text,password,hidden,file,textarea,select,radio,checkbox
*
*---------------Common validation checkform (oform) for client forms-----------------
*/
/**
* Regular expression Use cases
* 1, ^\s+[a-z a-z]$ can not be empty can not be a space can only be English letters
* 2, \s{6, can not be empty for more than six digits
* 3, ^\d+$ can not have a space can not be not a digital
* 4, (. *) (\.jpg|\.bmp) $ can only be JPG and BMP format
* 5, ^\d{4}\-\d{1,2}-\d{1,2}$ can only be 2004-10-22 format
* 6, ^0$ at least one of the selected
* 7, ^0{2,}$ select at least two
* 8, ^[\s|\s]{20,}$ can not be empty for more than 20 words
* 9, ^\+? [A-z0-9] (([-+.]| [_]+)? [a-z0-9]+) *@ ([A-z0-9]+ (\.| \-)) +[a-z]{2,6}$ Mail
* 10, \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * ([,;] \s*\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *) * Enter multiple addresses separate messages with commas or spaces
* 11, ^ ([0-9]+\)]? [0-9] {7,8}$ Phone number 7-bit or 8-bit or preceded by an area code such as (022) 87341628
* 12, ^[a-z-Z 0-9 _]+@[a-z-z 0-9 _]+ (\.[ A-Z 0-9 _]+) + (\,[a-z-Z 0-9 _]+@[a-z-z 0-9 _]+ (\.[ A-Z 0-9 _]+) + *$
* Can only be letters, numbers, underscores; must have @ and. Simultaneous format to standardize messages
* ^\w+@\w+ (\.\w+) + (\,\w+@\w+ (\.\w+) +) *$ The above expression can also be written in this way, more concise.
^\w+ ((-\w+) | ( \.\w+)) *\@\w+ (\.| -) *\.\w+$ \w+)
*/
////////////////////////////////////////////////////////////////////////////////
Main function
function Checkform (oform)
{
var els = oform.elements;
Traverse all table elements
for (Var i=0;i<els.length;i++)
{
Whether you need to verify
if (Els[i].check)
{
Gets the regular string of validation
var sreg = Els[i].check;
Gets the value of the form, using the universal value function
var sval = GetValue (Els[i]);
String-> regular expression, case-insensitive
var reg = new RegExp (Sreg, "I");
if (!reg.test (Sval))
{
Validation does not pass, pop-up prompts warning
alert (els[i].warning);
This form element gets the focus, with the universal return function
GoBack (Els[i])
return false;
}
}
}
}
The universal value function is divided into three classes to take the value
Text input box, direct value El.value
Tando, traverse all options to get the number of selected returns the result "00" indicates that two are selected
Single-Multiple Pull-down menu, traversing all options to get the selected number returns the result "0" indicates the selected
function GetValue (EL)
{
To get the type of a FORM element
var stype = El.type;
Switch (stype)
{
Case "Text":
Case "hidden":
Case "Password":
Case "File":
Case "textarea": return el.value;
Case "checkbox":
Case "Radio": Return Getvaluechoose (EL);
Case "Select-one":
Case "Select-multiple": Return Getvaluesel (EL);
}
Get the number of Radio,checkbox selected, with "0" to indicate the number of selected, we write regular time can be passed 0{1,} to indicate the number of selected
function Getvaluechoose (EL)
{
var svalue = "";
Gets the name of the first element and searches for this element group
var tmpels = Document.getelementsbyname (el.name);
for (Var i=0;i<tmpels.length;i++)
{
if (tmpels[i].checked)
{
svalue + = "0";
}
}
return svalue;
}
Get the select number of select, with "0" to indicate the number of selected, we write the regular time can pass 0{1,} to indicate the number of selected
function Getvaluesel (EL)
{
var svalue = "";
for (Var i=0;i<el.options.length;i++)
{
The Select Drop-down Box Prompt option is set to Value= ""
if (el.options[i].selected && el.options[i].value!= "")
{
svalue + = "0";
}
}
return svalue;
}
}
A universal return function that verifies the effect of not passing the return. Three classes to take the value
Text input box, cursor positioned at end of text input box
Tando, the first option gets the focus
Single Multi Pull-down menu, get focus
function GoBack (EL)
{
To get the type of a FORM element
var stype = El.type;
Switch (stype)
{
Case "Text":
Case "hidden":
Case "Password":
Case "File":
Case "textarea": El.focus (); var rng = El.createtextrange (); Rng.collapse (FALSE); Rng.select ();
Case "checkbox":
Case "Radio": var els = document.getelementsbyname (el.name); Els[0].focus ();
Case "Select-one":
Case "Select-multiple": El.focus ();
}
}
Check field
function CheckField (Field)
{
Whether you need to verify
if (Field.check)
{
Gets the regular string of validation
var sreg = Field.check;
Gets the value of the form, using the universal value function
var sval = GetValue (Field);
String-> regular expression, case-insensitive
var reg = new RegExp (Sreg);
if (!reg.test (Sval))
{
Validation does not pass, pop-up prompts warning
alert (field.warning);
This form element gets the focus, with the universal return function
GoBack (Field)
return false;
}
}
return true;
}