Edit the Form Verification class

Source: Internet
Author: User

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! ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.