本文執行個體講述了jQuery外掛程式formValidator自訂函數擴充功能的方法。分享給大家供大家參考,具體如下:
jQuery formValidator表單驗證外掛程式是什麼?感興趣的讀者可參考《jQuery formValidator表單驗證外掛程式》以及本站其他相關文檔
此處省略若干文字。
實際項目中的表單應用是多種多樣的,隨之而來的驗證也是多變的,但Jquery formValidator為我們提供了自訂函數介面,個人認為是其最主要的強大之處。廢話不多說,直接執行個體。
例一:有線電話和手機,至少選其一,可以不選。
分析:這屬於組合驗證,需要根據使用者選擇框體的不同進行不同的驗證條件。
知識點:Jquery formvalidator提供了自訂函數介面為functionValidator({ fun: funname });
有線電話手機
$("#txtMobileTel,#txtContactTel").formValidator({ tipid: "txtMobileTelTip", onshow: "請填寫任一種聯絡號碼", 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}-)?(0\d{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}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/) != -1) { return true } else { return "有線電話電話格式錯誤"; } } }};
例二:地區級聯下拉,當不存在二級地區的下拉解除校正。
省市地區級聯
$("#ddlOne").formValidator({ onshow: "請選擇省市", onfocus: "省市必須選擇", oncorrect: "輸入正確" }).inputValidator({ min: 1, onerror: "請選擇有效地區" }).functionValidator({ fun: city }); $("#ddlTwo").formValidator({ onshow: "請選擇城市", onfocus: "城市必須選擇", oncorrect: "輸入正確" }).inputValidator({ min: 1, onerror: "請選擇有效地區" });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); //恢複校正 } });}
常用驗證:
整數:
複製代碼 代碼如下:
$("#zs").formValidator({onshow:"請輸入整數",oncorrect:"謝謝你的合作,你的整數正確"}).regexValidator({regexp:"intege",datatype:"enum",onerror:"整數格式不正確"});
正整數:
複製代碼 代碼如下:
$("#zzs").formValidator({onshow:"請輸入正整數",oncorrect:"謝謝你的合作,你的正整數正確"}).regexValidator({regexp:"intege1",datatype:"enum",onerror:"正整數格式不正確"});
負整數:
複製代碼 代碼如下:
$("#fzs").formValidator({onshow:"請輸入負整數",oncorrect:"謝謝你的合作,你的負整數正確"}).regexValidator({regexp:"intege2",datatype:"enum",onerror:"負整數格式不正確"});
正數:
複製代碼 代碼如下:
$("#zs1").formValidator({onshow:"請輸入正數",oncorrect:"謝謝你的合作,你的正數正確"}).regexValidator({regexp:"num1",datatype:"enum",onerror:"正數格式不正確"});
數字:
複製代碼 代碼如下:
$("#sz").formValidator({onshow:"請輸入數字",oncorrect:"謝謝你的合作,你的數字正確"}).regexValidator({regexp:"num",datatype:"enum",onerror:"數字格式不正確"});
負數:
複製代碼 代碼如下:
$("#fs").formValidator({onshow:"請輸入負數",oncorrect:"謝謝你的合作,你的負數正確"}).regexValidator({regexp:"num2",datatype:"enum",onerror:"負數格式不正確"});
浮點數:
$("#zfds").formValidator({onshow:"請輸入正浮點數",oncorrect:"謝謝你的合作,你的正浮點數正確"}).regexValidator({regexp:"decmal1",datatype:"enum",onerror:"正浮點數格式不正確"});$("#ffds").formValidator({onshow:"請輸入負浮點數",oncorrect:"謝謝你的合作,你的負浮點數正確"}).regexValidator({regexp:"decmal2",datatype:"enum",onerror:"負浮點數格式不正確"});$("#fffds").formValidator({onshow:"請輸入非負浮點數",oncorrect:"謝謝你的合作,你的非負浮點數正確"}).regexValidator({regexp:"decmal4",datatype:"enum",onerror:"非負浮點數格式不正確"});$("#fzfds").formValidator({onshow:"請輸入非正浮點數",oncorrect:"謝謝你的合作,你的非正浮點數正確"}).regexValidator({regexp:"decmal5",datatype:"enum",onerror:"非正浮點數格式不正確"});
手機:
複製代碼 代碼如下:
$("#sj").formValidator({onshow:"請輸入你的手機號碼",onfocus:"必須是13或15打頭哦",oncorrect:"謝謝你的合作,你的手機號碼正確"}).regexValidator({regexp:"mobile",datatype:"enum",onerror:"手機號碼格式不正確"});
有線電話:
複製代碼 代碼如下:
$("#dh").formValidator({onshow:"請輸入國內電話",onfocus:"例如:0577-88888888或省略區號88888888",oncorrect:"謝謝你的合作,你的國內電話正確"}).regexValidator({regexp:"tel",datatype:"enum",onerror:"國內電話格式不正確"});
郵箱:
複製代碼 代碼如下:
$("#email").formValidator({onshow:"請輸入你的email",onfocus:"請注意你輸入的email格式,例如:wzmaodong@126.com",oncorrect:"謝謝你的合作,你的email正確"}).regexValidator({regexp:"email",datatype:"enum",onerror:"email格式不正確"});
郵編:
複製代碼 代碼如下:
$("#yb").formValidator({onshow:"請輸入郵編",onfocus:"6位元字組成的哦",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程式設計有所協助。