Autor: edit the prodigal son
From: http://bbs.51js.com/thread-68161-1-1.html
// Form Verification class
Function ValidatorClass ()
{
Var IsError = false;
// Check the name. Only Chinese characters, letters, numbers, and underscores can be entered.
This. ChkName = function (obj, msg)
{
If (IsError) return;
If (obj. value. trim (). length <2 | (/[^ u4e00-u9fa5w]/. test (obj. value. trim () ErrorHandle (obj, msg );
}
// Check the email address
This. ChkEmail = function (obj, msg)
{
If (IsError) return;
If (! /^ W + @ w + .(? : Com | cn | org | net | cc | TV | info | com.cn | net.cn | org.cn | gov.cn) $/I. test (obj. value. trim () ErrorHandle (obj, msg );
}
// Check the place name. It can only be Chinese and cannot be blank.
This. ChkPlace = function (obj, msg)
{
If (IsError) return;
If (obj. value. trim (). length <1 | (/[^ u4e00-u9fa5]/. test (obj. value. trim () ErrorHandle (obj, msg );
}
// Check the detailed address
This. ChkAddress = function (obj, msg)
{
If (IsError) return;
If (obj. value. Trim (). length <2) ErrorHandle (obj, msg );
}
// Check the email code
This. ChkPostNumber = function (obj, msg)
{
If (IsError) return;
If (! (/^ D {6} $/. test (obj. value. Trim () ErrorHandle (obj, msg );
}
// Check the mobile phone number
This. ChkMobile = function (obj, msg)
{
If (IsError) return;
If (! (/^ (? : 13d | 159 )-? D {5} (d {3} | * {3}) $/. test (obj. value. Trim () ErrorHandle (obj, msg );
}
// Check the landline number
This. ChkPhone = function (obj, msg)
{
If (IsError) return;
If (! (/^ D {3, 4 }-? D {4, 5} (d {3} | * {3}) $/. test (obj. value. Trim () ErrorHandle (obj, msg );
}
// Submit a form event
This. Submit = function (Form, msg)
{
If (IsError) return;
If (msg) alert (msg );
Form. submit ();
}
// Handle errors
Function ErrorHandle (obj, msg)
{
Alert (msg );
IsError = true;
Obj. focus ();
}
}
Application instance:
Function ChkForm ()
{
Var Form = document. TestForm;
Var Validator = new ValidatorClass ();
Validator. ChkName (Form. ZD_UserName, "the orderer name is invalid! ");
Validator. ChkEmail (Form. ZD_Email, "the order recipient's email address is invalid! ");
Validator. ChkPlace (Form. ZD_Province, "the province of the orderer is invalid! ");
Validator. ChkPlace (Form. ZD_City, "the ordering city is invalid! ");
Validator. ChkAddress (Form. ZD_Address, "the orderer address is invalid! ");
Validator. ChkPostNumber (Form. ZD_Zip, "the orderer's zip code is invalid! ");
Validator. ChkMobile (Form. ZD_Mobile, "the order mobile phone number is invalid! ");
Validator. ChkPhone (Form. ZD_Phone, "the landline number of the orderer is invalid! ");
Validator. Submit (Form, "Verification Successful! ");
}