Home  >  Article  >  Web Front-end  >  Edit the prodigal version of the form validation class_javascript skills

Edit the prodigal version of the form validation class_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:13:42936browse

Autor: Editor Prodigal
From:http://bbs.51js.com/thread-68161-1-1.html
//Form Validation Class
function ValidatorClass()
{
var IsError = false;

//Check the name, only Chinese, letters, numbers, and underscores can be entered
this.ChkName = function(obj,msg)
{
if (IsError) return;
if (obj.value.Trim().length }

//Check 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 in Chinese and cannot be empty
this.ChkPlace = function(obj,msg)
{
if (IsError) return;
if (obj.value.Trim().length }

//Check the detailed address
this.ChkAddress = function (obj,msg)
{
if (IsError) return;
if ( obj.value.Trim().length }

//Check the email encoding
this.ChkPostNumber = function (obj,msg)
{
if (IsError) return;
if(!(/^d{6}$/.test(obj.value.Trim()))) ErrorHandle(obj,msg);
}

//Check mobile 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 form event
this.Submit = function (Form,msg)
{
if (IsError) return;
if (msg) alert(msg);
Form.submit ();
}

//Error handling
function ErrorHandle(obj,msg)
{
alert(msg);
IsError = true;
obj .focus();
}
}
Application example:
function ChkForm()
{
var Form = document.TestForm;
var Validator = new ValidatorClass() ;
Validator.ChkName(Form.ZD_UserName,"The orderer's name is illegal!");
Validator.ChkEmail(Form.ZD_Email,"The orderer's email address is illegal!");
Validator.ChkPlace (Form.ZD_Province,"The province of the orderer is illegal!");
Validator.ChkPlace(Form.ZD_City,"The city of the orderer is illegal!");
Validator.ChkAddress(Form.ZD_Address,"The orderer The address is illegal!");
Validator.ChkPostNumber(Form.ZD_Zip,"The orderer's postal code is illegal!");
Validator.ChkMobile(Form.ZD_Mobile,"The orderer's mobile phone number is illegal!");
Validator.ChkPhone(Form.ZD_Phone,"The orderer's landline phone number is illegal!");
Validator.Submit(Form,"Verification successful!" ");
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn