本文實例講述了jQuery插件formValidator自定義函數(shù)擴展功能的方法。分享給大家供大家參考,具體如下:
jQuery formValidator表單驗證插件是什么?感興趣的讀者可參考《jQuery formValidator表單驗證插件》以及本站其他相關(guān)文檔
此處省略若干文字。
實際項目中的表單應用是多種多樣的,隨之而來的驗證也是多變的,但Jquery formValidator為我們提供了自定義函數(shù)接口,個人認為是其最主要的強大之處。廢話不多說,直接實例。
例一:座機和手機,至少選其一,可以不選。
分析:這屬于組合驗證,需要根據(jù)用戶選擇框體的不同進行不同的驗證條件。
知識點:Jquery formvalidator提供了自定義函數(shù)接口為functionValidator({ fun: funname });
座機手機
$("#txtMobileTel,#txtContactTel").formValidator({ tipid: "txtMobileTelTip", onshow: "請?zhí)顚懭我环N聯(lián)系號碼", onfocus: "請輸入移動電話或座機電話", oncorrect: "輸入正確!" }).functionValidator({ fun: allEmpty }); function allEmpty(val, elem) { if ($("#txtMobileTel").val() == "" && $("#txtContactTel").val() == "") { return '請輸入移動電話或座機電話'; } else { if ($("#txtMobileTel").val() != "" && $("#txtContactTel").val() != "") { if (($("#txtMobileTel").val()).search(/^(((13[0-9]{1})|(15[0-9]{1}))+d{8})$/) != -1) { if (($("#txtContactTel").val()).search(/^(([0+]d{2,3}-)?(0d{2,3})-)?(d{7,8})(-(d{3,}))?$/) != -1) { return true } else { return "座機電話格式錯誤"; } } else { return "移動電話格式錯誤"; } } else { if ($("#txtMobileTel").val() != "") { if (($("#txtMobileTel").val()).search(/^(((13[0-9]{1})|(15[0-9]{1}))+d{8})$/) != -1) { return true } else { return "移動電話格式錯誤"; } } if ($("#txtContactTel").val() != "") { if (($("#txtContactTel").val()).search(/^(([0+]d{2,3}-)?(0d{2,3})-)?(d{7,8})(-(d{3,}))?$/) != -1) { return true } else { return "座機電話格式錯誤"; } } } };
例二:地區(qū)級聯(lián)下拉,當不存在二級地區(qū)的下拉解除校驗。
省市地區(qū)級聯(lián)
$("#ddlOne").formValidator({ onshow: "請選擇省市", onfocus: "省市必須選擇", oncorrect: "輸入正確" }).inputValidator({ min: 1, onerror: "請選擇有效的地區(qū)" }).functionValidator({ fun: city }); $("#ddlTwo").formValidator({ onshow: "請選擇城市", onfocus: "城市必須選擇", oncorrect: "輸入正確" }).inputValidator({ min: 1, onerror: "請選擇有效的地區(qū)" }); function city(val, elem) { var a = ""; $.getJSON("../Customer/Area.ashx?parentid=" + $("#ddlOne option:selected").val(), null, function(json) { if (json[0].areacode == "0") { $("#ddlTwo").attr("disabled", true).unFormValidator(true); //解除校驗 } else { $("#ddlTwo").attr("disabled", false).unFormValidator(false); //恢復校驗 } }); }
常用驗證:
整數(shù):
$("#zs").formValidator({onshow:"請輸入整數(shù)",oncorrect:"謝謝你的合作,你的整數(shù)正確"}).regexValidator({regexp:"intege",datatype:"enum",onerror:"整數(shù)格式不正確"});
正整數(shù):
$("#zzs").formValidator({onshow:"請輸入正整數(shù)",oncorrect:"謝謝你的合作,你的正整數(shù)正確"}).regexValidator({regexp:"intege1",datatype:"enum",onerror:"正整數(shù)格式不正確"});
負整數(shù):
$("#fzs").formValidator({onshow:"請輸入負整數(shù)",oncorrect:"謝謝你的合作,你的負整數(shù)正確"}).regexValidator({regexp:"intege2",datatype:"enum",onerror:"負整數(shù)格式不正確"});
正數(shù):
$("#zs1").formValidator({onshow:"請輸入正數(shù)",oncorrect:"謝謝你的合作,你的正數(shù)正確"}).regexValidator({regexp:"num1",datatype:"enum",onerror:"正數(shù)格式不正確"});
數(shù)字:
$("#sz").formValidator({onshow:"請輸入數(shù)字",oncorrect:"謝謝你的合作,你的數(shù)字正確"}).regexValidator({regexp:"num",datatype:"enum",onerror:"數(shù)字格式不正確"});
負數(shù):
$("#fs").formValidator({onshow:"請輸入負數(shù)",oncorrect:"謝謝你的合作,你的負數(shù)正確"}).regexValidator({regexp:"num2",datatype:"enum",onerror:"負數(shù)格式不正確"});
浮點數(shù):
$("#zfds").formValidator({onshow:"請輸入正浮點數(shù)",oncorrect:"謝謝你的合作,你的正浮點數(shù)正確"}).regexValidator({regexp:"decmal1",datatype:"enum",onerror:"正浮點數(shù)格式不正確"}); $("#ffds").formValidator({onshow:"請輸入負浮點數(shù)",oncorrect:"謝謝你的合作,你的負浮點數(shù)正確"}).regexValidator({regexp:"decmal2",datatype:"enum",onerror:"負浮點數(shù)格式不正確"}); $("#fffds").formValidator({onshow:"請輸入非負浮點數(shù)",oncorrect:"謝謝你的合作,你的非負浮點數(shù)正確"}).regexValidator({regexp:"decmal4",datatype:"enum",onerror:"非負浮點數(shù)格式不正確"}); $("#fzfds").formValidator({onshow:"請輸入非正浮點數(shù)",oncorrect:"謝謝你的合作,你的非正浮點數(shù)正確"}).regexValidator({regexp:"decmal5",datatype:"enum",onerror:"非正浮點數(shù)格式不正確"});
手機:
$("#sj").formValidator({onshow:"請輸入你的手機號碼",onfocus:"必須是13或15打頭哦",oncorrect:"謝謝你的合作,你的手機號碼正確"}).regexValidator({regexp:"mobile",datatype:"enum",onerror:"手機號碼格式不正確"});
座機:
$("#dh").formValidator({onshow:"請輸入國內(nèi)電話",onfocus:"例如:0577-88888888或省略區(qū)號88888888",oncorrect:"謝謝你的合作,你的國內(nèi)電話正確"}).regexValidator({regexp:"tel",datatype:"enum",onerror:"國內(nèi)電話格式不正確"});
郵箱:
$("#email").formValidator({onshow:"請輸入你的email",onfocus:"請注意你輸入的email格式,例如:wzmaodong@126.com",oncorrect:"謝謝你的合作,你的email正確"}).regexValidator({regexp:"email",datatype:"enum",onerror:"email格式不正確"});
郵編:
$("#yb").formValidator({onshow:"請輸入郵編",onfocus:"6位數(shù)字組成的哦",oncorrect:"謝謝你的合作,你的郵編正確"}).regexValidator({regexp:"zipcode",datatype:"enum",onerror:"郵編格式不正確"});
QQ:
$("#qq").formValidator({onshow:"請輸入QQ號碼",oncorrect:"謝謝你的合作,你的QQ號碼正確"}).regexValidator({regexp:"qq",datatype:"enum",onerror:"QQ號碼格式不正確"});
身份證:
$("#sfz").formValidator({onshow:"請輸入身份證",oncorrect:"謝謝你的合作,你的身份證正確"}).regexValidator({regexp:"idcard",datatype:"enum",onerror:"身份證格式不正確"});
字母:
$("#zm").formValidator({onshow:"請輸入字母",oncorrect:"謝謝你的合作,你的字母正確"}).regexValidator({regexp:"letter",datatype:"enum",onerror:"字母格式不正確"});
大寫字母:
$("#dxzm").formValidator({onshow:"請輸入大寫字母",oncorrect:"謝謝你的合作,你的大寫字母正確"}).regexValidator({regexp:"letter_u",datatype:"enum",onerror:"大寫字母格式不正確"});
小寫字母:
$("#xxzm").formValidator({onshow:"請輸入小寫字母",oncorrect:"謝謝你的合作,你的小寫字母正確"}).regexValidator({regexp:"letter_l",datatype:"enum",onerror:"小寫字母格式不正確"});
希望本文所述對大家jQuery程序設計有所幫助。