编辑浪子版表单验证类_Javascript教程-查字典教程网
编辑浪子版表单验证类
编辑浪子版表单验证类
发布时间:2016-12-30 来源:查字典编辑
摘要:Autor:编辑浪子From:http://bbs.51js.com/thread-68161-1-1.html//表单验证类functio...

Autor:编辑浪子

From:http://bbs.51js.com/thread-68161-1-1.html

//表单验证类

functionValidatorClass()

{

varIsError=false;

//检查姓名,只能输入中文、字母、数字、下划线

this.ChkName=function(obj,msg)

{

if(IsError)return;

if(obj.value.Trim().length<2||(/[^u4e00-u9fa5w]/.test(obj.value.Trim())))ErrorHandle(obj,msg);

}

//检查邮件地址

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);

}

//检查地名,只能是中文,并不能为空

this.ChkPlace=function(obj,msg)

{

if(IsError)return;

if(obj.value.Trim().length<1||(/[^u4e00-u9fa5]/.test(obj.value.Trim())))ErrorHandle(obj,msg);

}

//检查详细地址

this.ChkAddress=function(obj,msg)

{

if(IsError)return;

if(obj.value.Trim().length<2)ErrorHandle(obj,msg);

}

//检查邮件编码

this.ChkPostNumber=function(obj,msg)

{

if(IsError)return;

if(!(/^d{6}$/.test(obj.value.Trim())))ErrorHandle(obj,msg);

}

//检查手机号码

this.ChkMobile=function(obj,msg)

{

if(IsError)return;

if(!(/^(?:13d|159)-?d{5}(d{3}|*{3})$/.test(obj.value.Trim())))ErrorHandle(obj,msg);

}

//检查固定电话号码

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);

}

//提交表单事件

this.Submit=function(Form,msg)

{

if(IsError)return;

if(msg)alert(msg);

Form.submit();

}

//错误处理

functionErrorHandle(obj,msg)

{

alert(msg);

IsError=true;

obj.focus();

}

}

应用实例:

functionChkForm()

{

varForm=document.TestForm;

varValidator=newValidatorClass();

Validator.ChkName(Form.ZD_UserName,"订货人姓名不合法!");

Validator.ChkEmail(Form.ZD_Email,"订货人邮件地址不合法!");

Validator.ChkPlace(Form.ZD_Province,"订货人省份不合法!");

Validator.ChkPlace(Form.ZD_City,"订货人城市不合法!");

Validator.ChkAddress(Form.ZD_Address,"订货人地址不合法!");

Validator.ChkPostNumber(Form.ZD_Zip,"订货人邮编不合法!");

Validator.ChkMobile(Form.ZD_Mobile,"订货人移动电话不合法!");

Validator.ChkPhone(Form.ZD_Phone,"订货人固定电话不合法!");

Validator.Submit(Form,"验证成功!");

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类