During use, save the following JavaScript code to a single JS file.
1. Form requirements
<Form name = "formname" onsubmit = "Return validateform (this)"> </form>
All the following types of fields in the form are verified in sequence, and all the verifications except the leading and suffix spaces. Note that they are case sensitive.
2. null value verification
If the emptyinfo attribute is added to any field in the form, whether the field is empty is verified (it can be used together with the maximum length verification \ general verification method ).
If this attribute is not specified, this field allows null values.
For example, the <input type = "text" name = "fieldnamename" emptyinfo = "field cannot be blank! ">
3. Maximum length verification (can be used together with null value verification and general verification methods ):
<Input type = "text" name = "fieldnamename" maxlength = "20" lengthinfo = "the maximum length cannot exceed 20! ">
Or, <textarea maxlength = "2000" lengthinfo = "the maximum length cannot exceed 2000! ">
3. General verification method (do not verify null values ):
For example, <input type = "text" validator = "^ (19 | 20) [0-9] {2} $" errorinfo = "Incorrect year! ">
4. Standard verification (not used together with other verification methods ):
All are implemented through <input type = "hidden">, and the name attribute is not required to submit to the server.
4.1 Legal date Verification:
<Input type = "text" name = "yearfieldname" value = "2004"> note: this can also be <select name = "yearfieldname"> </SELECT>, which is the same
<Input type = "text" name = "monthfieldname" value = "02">
<Input type = "text" name = "dayfieldname" value = "03">
<Input type = "hidden" validatortype = "dategroup" year = "yearfieldname" month = "monthfieldname" Day = "dayfieldname" errorinfo = "incorrect date! ">
Yearfieldname, monthfieldname, and dayfieldname are the year, month, and day fields. The month and day fields can be in two (mm) or one-digit format (m ),
Each field is not verified separately here (if you want to test, please use the previous general verification method for the three fields on the year, month, and day), and only check whether the maximum value of the date is valid;
4.2. Date Format verification (please note that this verification does not verify whether the date is valid, and the method for obtaining the year, month, and day data from the format is not found ^_^ ):
<Input type = "text" name = "datefieldname" value = "21:31:00">
<Input type = "hidden" validatortype = "date" fieldname = "datefieldname"; format = "yyyy-mm-dd hh: mm: SS" errorinfo = "incorrect date! ">
The format only supports y, M, D, H, M, and S (other characters are considered non-time characters)
4.3 list Verification:
Check whether at least one record is selected in the list (checkbox, redio, select) (select is mainly used for Multiple choices)
<Input type = "checkbox" name = "checkbox1">
<Input type = "hidden" validatortype = "checkbox" fieldname = "checkbox1" errorinfo = "Please select at least one record! ">
Here, validatortype can be checkbox, R, select;
For a select form, if you want to select a record that cannot be the first record, use the following method:
<Select name = "select1" emptyinfo = "select an option! ">
<Option value = "" >== select ==</option>
<Option value = "1"> 1 </option>
<SELECT>
4.4. Email Verification:
<Input type = "text" name = "email">
<Input type = "hidden" fieldname = "email" validatortype = "email" separator = "," errorinfo = "Incorrect email! ">
Here, separator is optional, indicating the separator when multiple emails are input (none) (this option can only be one address)
4.5 Add other JavaScript operations:
<SCRIPT type = "text/JavaScript">
Function functionname (){
Custom Method
}
</SCRIPT>
<Input type = "hidden" validatortype = "JavaScript" functionname = "functionname"> (in this case, attributes such as emptyinfo are invalid)
The JavaScript method specified in the function property is called. (If the method is required to return true or false, false is returned, and the form is not verified or submitted ).
5. disable a button before the form passes verification and submission (other fields can also be disable and cannot be in the same field as other verifications). The button is not required to be the last one in the form.
<Input type = "button" name = "Submit" validatortype = "Disable">
6. Do not verify the form
<Input type = "hidden" name = "Validate" value = "0" functionname = "functionname">
If the validator field value is 0, the form is not verified. directly submit the form or execute the specified function and return true. Then, the form is submitted.
Functionname is optional
-->
<SCRIPT type = "text/JavaScript">
Function getstringlength (STR ){
VaR endvalue = 0;
VaR sourcestr = new string (STR );
VaR tempstr;
For (VAR strposition = 0; strposition <sourcestr. length; strposition ++ ){
Tempstr = sourcestr. charat (strposition );
If (tempstr. charcodeat (0)> 255 | tempstr. charcodeat (0) <0 ){
Endvalue = endvalue + 2;
} Else {
Endvalue = endvalue + 1;
}
}
Return (endvalue );
}
Function trim (STR ){
If (STR = NULL) Return "";
If (Str. Length = 0) Return "";
VaR I = 0, j = Str. Length-1, C;
For (; I <Str. length; I ++ ){
C = Str. charat (I );
If (C! = '') Break;
}
For (; j>-1; j --){
C = Str. charat (j );
If (C! = '') Break;
}
If (I> J) Return "";
Return Str. substring (I, j + 1 );
}
Function validatedate (date, format, alt ){
VaR time = trim (date. value );
If (time = "") return;
VaR Reg = format;
VaR Reg = reg. Replace (/YYYY/, "[0-9] {4 }");
VaR Reg = reg. Replace (/yy/, "[0-9] {2 }");
VaR Reg = reg. Replace (/MM/, "(0 [1-9]) | 1 [0-2])");
VaR Reg = reg. Replace (/M/, "([1-9]) | 1 [0-2])");
VaR Reg = reg. replace (/DD/, "(0 [1-9]) | ([1-2] [0-9]) | 30 | 31 )");
VaR Reg = reg. Replace (/D/, "([1-9] | [1-2] [0-9] | 30 | 31 ))");
VaR Reg = reg. Replace (/hh/, "([0-1] [0-9]) | 20 | 21 | 22 | 23 )");
VaR Reg = reg. Replace (/h/, "([0-9] | 1 [0-9] | 20 | 21 | 22 | 23 )");
VaR Reg = reg. Replace (/MM/, "([0-5] [0-9])");
VaR Reg = reg. Replace (/M/, "([0-9] | ([1-5] [0-9])");
VaR Reg = reg. Replace (/SS/, "([0-5] [0-9])");
VaR Reg = reg. Replace (/S/, "([0-9] | ([1-5] [0-9])");
Reg = new Regexp ("^" + Reg + "$ ");
If (Reg. Test (time) = false) {// verify that the format is valid
Alert (ALT );
Date. Focus ();
Return false;
}
Return true;
}
Function validatedategroup (year, month, day, alt ){
VaR array = new array (, 31 );
Var y = parseint (Year. value );
VaR M = parseint (month. value );
VaR d = parseint (day. value );
VaR maxday = array M-1];
If (M = 2 ){
If (Y % 4 = 0 & Y % 100! = 0) | Y % 400 = 0 ){
Maxday = 29;
}
}
If (D> maxday ){
Alert (ALT );
Return false;
}
Return true;
}
Function validatecheckbox (OBJ, alt ){
VaR rs = false;
If (OBJ! = NULL ){
If (obj. Length = NULL ){
Return obj. checked;
}
For (I = 0; I <obj. length; I ++ ){
If (OBJ [I]. Checked = true ){
Return true;
}
}
}
Alert (ALT );
Return Rs;
}
Function validateradio (OBJ, alt ){
VaR rs = false;
If (OBJ! = NULL ){
If (obj. Length = NULL ){
Return obj. checked;
}
For (I = 0; I <obj. length; I ++ ){
If (OBJ [I]. Checked = true ){
Return true;
}
}
}
Alert (ALT );
Return Rs;
}
Function validateselect (OBJ, alt ){
VaR rs = false;
If (OBJ! = NULL ){
For (I = 0; I <obj. Options. length; I ++ ){
If (obj. Options [I]. Selected = true ){
Return true;
}
}
}
Alert (ALT );
Return Rs;
}
Function validateemail (email, ALT, separator ){
VaR mail = trim (email. value );
If (Mail = "") return;
Var em;
VaR myreg =/^ [_ a-z0-9] + @ ([_ a-z0-9] + \.) + [a-z0-9] {2, 3} $ /;
If (separator = NULL ){
If (myreg. Test (email. Value) = false ){
Alert (ALT );
Email. Focus ();
Return false;
}
}
Else {
Em = Email. value. Split (separator );
For (I = 0; I <em. length; I ++ ){
Em [I] = em [I]. Trim ();
If (EM [I]. length> 0 & myreg. Test (EM [I]) = false ){
Alert (ALT );
Email. Focus ();
Return false;
}
}
}
Return true;
}
Function validateform (theform) {// returns true if the verification succeeds
VaR disablelist = new array ();
VaR field = theform. elements; // put all elements in the form into an array
For (VAR I = 0; I <field. length; I ++ ){
VaR vali = theform. Validate;
If (Vali! = NULL ){
If (VALI. value = "0 "){
VaR fun = vali. functionname;
If (fun! = NULL ){
Return eval (fun + "()");
}
Else {
Return true;
}
}
}
VaR empty = false;
VaR value = trim (field [I]. value );
If (value. Length = 0) {// whether the value is null
Empty = true;
}
VaR emptyinfo = field [I]. emptyinfo; // null value verification
If (emptyinfo! = NULL & empty = true ){
Alert (emptyinfo );
Field [I]. Focus ();
Return false;
}
VaR lengthinfo = field [I]. lengthinfo; // maximum length Verification
If (lengthinfo! = NULL & getstringlength (value)> Field [I]. maxlength ){
Alert (lengthinfo );
Field [I]. Focus ();
Return false;
}
VaR validatortype = field [I]. validatortype;
If (validatortype! = NULL) {// other Javascript
VaR rs = true;
If (validatortype = "JavaScript "){
Eval ("rs =" + field [I]. functionname + "()");
If (rs = false ){
Return false;
}
Else {
Continue;
}
}
Else if (validatortype = "Disable") {// The Disable button before submitting the form
Disablelist. Length ++;
Disablelist [disablelist. Length-1] = field [I];
Continue;
}
Else if (validatortype = "date "){
Rs = validatedate (theform. Elements (field [I]. fieldname), field [I]. format, field [I]. errorinfo );
}
Else if (validatortype = "dategroup "){
Rs = validatedategroup (theform. elements (field [I]. year), theform. elements (field [I]. month), theform. elements (field [I]. day), field [I]. errorinfo );
}
Else if (validatortype = "checkbox "){
Rs = validatecheckbox (theform. Elements (field [I]. fieldname), field [I]. errorinfo );
}
Else if (validatortype = "radio "){
Rs = validateradio (theform. Elements (field [I]. fieldname), field [I]. errorinfo );
}
Else if (validatortype = "select "){
Rs = validateselect (theform. Elements (field [I]. fieldname), field [I]. errorinfo );
}
Else if (validatortype = "email "){
Rs = validateemail (theform. Elements (field [I]. fieldname), field [I]. errorinfo );
}
Else {
Alert ("the verification type is not supported, fieldname:" + field [I]. Name );
Return false;
}
If (rs = false ){
Return false;
}
}
Else {// general verification
If (empty = false ){
VaR v = field [I]. validator; // obtain its validator attribute
If (! V) continue; // if this attribute does not exist, ignore the current element.
VaR Reg = new Regexp (v );
If (Reg. Test (field [I]. Value) = false ){
Alert (field [I]. errorinfo );
Field [I]. Focus ();
Return false;
}
}
}
}
For (I = 0; I <disablelist. length; I ++ ){
Disablelist [I]. Disabled = true;
}
Return true;
}
</SCRIPT>