Autor:编辑浪子
From:http://bbs.51js.com/thread-68161-1-1.html
//表单验证类
function ValidatorClass()
{
var IsError = 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();
}
//错误处理
function ErrorHandle(obj,msg)
{
alert(msg);
IsError = true;
obj.focus();
}
}
应用实例:
function ChkForm()
{
var Form = document.TestForm;
var Validator = new ValidatorClass();
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,"验证成功!");
}
编辑浪子版表单验证类
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@