|
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,"驗證成功!");
}
JavaScript技術:編輯浪子版表單驗證類,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。