富贵资源网 Design By www.hznty.com
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,"验证成功!");
}
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,"验证成功!");
}
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...