Home > Article > Web Front-end > Detailed explanation of jQuery plug-in formValidator custom function extension function example_jquery
The example in this article describes how to extend the functions of the jQuery plug-in formValidator custom function. Share it with everyone for your reference, the details are as follows:
What is the jQuery formValidator form validation plug-in? Interested readers can refer to "jQuery formValidator form validation plug-in" and other related documents on this site
Some text is omitted here.
The form applications in actual projects are diverse, and the verification that comes with it is also varied, but Jquery formValidator provides us with a custom function interface, which I personally think is its main strength. Without further ado, let’s go straight to examples.
Example 1: Landline or mobile phone, choose at least one, you don’t have to choose either.
Analysis: This is a combination verification, which requires different verification conditions according to the different frames selected by the user.
Knowledge point: Jquery formvalidator provides a custom function interface as functionValidator({ fun: funname });
Landline mobile phone
$("#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 "座机电话格式错误"; } } } };
Example 2: Region cascading drop-down, when there is no secondary region drop-down to cancel verification.
Province, city and region cascade
$("#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); //恢复校验 } }); }
Commonly used verification:
Integer:
Positive integer:
Negative integers:
Positive numbers:
Number:
Negative numbers:
Floating point number:
$("#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:"非正浮点数格式不正确"});
Mobile phone:
Landline:
Email:
Postcode:
QQ:
ID card:
Letter:
Capital letters:
Lower case letters:
I hope this article will be helpful to everyone in jQuery programming.