Javascript 自定义jQuery验证规则无法正常工作
我尝试使用自定义jQuery验证方法,如下所示:Javascript 自定义jQuery验证规则无法正常工作,javascript,jquery,html,jquery-plugins,Javascript,Jquery,Html,Jquery Plugins,我尝试使用自定义jQuery验证方法,如下所示: jQuery.validator.addMethod("selectnic", function(value, element){ if (/^[0-9]{9}[vVxX]$/.test(value)) { return true; // FAIL validation when REGEX matches } else { return false; // PASS validation o
jQuery.validator.addMethod("selectnic", function(value, element){
if (/^[0-9]{9}[vVxX]$/.test(value)) {
return true; // FAIL validation when REGEX matches
} else {
return false; // PASS validation otherwise
};
}, "wrong nic number");
<div class="contact-input-field">
<input type="text" class="input-text span2" id="contactpersonen_functie" name="contactpersonen_functie"></input>
</div>
jQuery.validator.addMethod("selectnic", function(value, element){
return !/^[0-9]{9}[vVxX]$/.test(value)
}, "wrong nic number");
这是规则:
contactpersonen_functie: {
selectnic: true,
required: true
},
HTML如下所示:
jQuery.validator.addMethod("selectnic", function(value, element){
if (/^[0-9]{9}[vVxX]$/.test(value)) {
return true; // FAIL validation when REGEX matches
} else {
return false; // PASS validation otherwise
};
}, "wrong nic number");
<div class="contact-input-field">
<input type="text" class="input-text span2" id="contactpersonen_functie" name="contactpersonen_functie"></input>
</div>
jQuery.validator.addMethod("selectnic", function(value, element){
return !/^[0-9]{9}[vVxX]$/.test(value)
}, "wrong nic number");
职能
这是整个脚本:
<![CDATA[
$(document).ready(function()
{
jQuery.validator.addMethod("selectnic", function(value, element){
return !/^[0-9]{9}[vVxX]$/.test(value)
}, "wrong nic number");
// Form validation
$(".klantregistratie form").validate({
rules: {
verploegen_form_klantregistratie_Bedrijfsnaam: "required",
verploegen_form_klantregistratie_Postcode:{
required:true,
maxlength:6
},
verploegen_form_klantregistratie_Plaats: "required",
contactpersonen_voornaam: "required",
contactpersonen_achternaam: "required",
contactpersonen_functie:{required:true, maxlength:30},
verploegen_form_klantregistratie_Emailadres_digitale_factuur:{
email:true
},
verploegen_form_klantregistratie_Telefoon_vast:{
required:true,
minlength:10,
maxlength:10,
digits:true
},
verploegen_form_klantregistratie_Emailadres: {
required: true,
email: true
},
contactpersonen_telefoon:{
required:true,
minlength:10,
maxlength:10,
digits:true
},
contactpersonen_email:{
required:true,
email:true
},
verploegen_form_klantregistratie_KvK_nummer:{
required:true,
minlength:8,
maxlength:8,
digits: true
},
verploegen_form_klantregistratie_naam_eigenaar: "required",
verploegen_form_klantregistratie_Telefoon_mobiel: "required"
},
contactpersonen_voornaam:"required",
contactpersonen_achternaam:"required",
contactpersonen_functie:{
selectnic:true,
required:true
},
contactpersonen_email1:{
required:true,
email:true
},
messages: {
verploegen_form_klantregistratie_Bedrijfsnaam: "De bedrijfsnaam is niet ingevuld",
verploegen_form_klantregistratie_Postcode: {
required: "De postcode is niet gevuld",
maxlength:"Postcode kan niet langer dan 6 tekens zijn"
},
//if(document.getElementById('contactpersonen_canorder_0').checked) { return true; } else { alert('please agree'); return false; }
verploegen_form_klantregistratie_Plaats: "De plaats is niet ingevuld",
verploegen_form_klantregistratie_Emailadres: {
required: "Je hebt geen email adres ingevuld",
email: "Je hebt geen geldig email adres ingevuld"
},
verploegen_form_klantregistratie_Emailadres_digitale_factuur:{
email: "Geen geldig email adres"
},
verploegen_form_klantregistratie_Telefoon_vast:{
digits:"Telefoon nummer bestaat uit 10 cijfers"
},
contactpersonen_telefoon:{
required: "U heeft geen tele ingevoerd",
digits:"Telefoon nummer bestaat enkel uit 10 cijfers"
},
contactpersonen_email1:{
required: "U heeft geen geldig email adres ingevuld",
email: "je email is leeg"
},
contactpersonen_voornaam:"U heeft u voornaam niet ingevuld",
contactpersonen_achternaam:"U heeft u achternaam niet ingevuld",
//contactpersonen_functie:{required:"U heeft niks ingevuld", maxlength:"Functie naam mag niet meer dan 30 karakters bevatten"},
verploegen_form_klantregistratie_KvK_nummer:{
required: "Het KVK nummer is niet ingevuld",
digits:"KVK nummer bestaat uit 8 nummers"
},
verploegen_form_klantregistratie_naam_eigenaar: "De naam van de eigenaar is niet ingevuld",
verploegen_form_klantregistratie_Telefoon_mobiel: "Het (mobiele) telefoon nummer is niet ingevuld"
}
});
您的逻辑是反向的;当验证通过时,您定义的方法应返回true
,当验证失败时,应返回false
。还请注意,在使用反转布尔值之后,可以直接返回测试()的结果。
代码>。试试这个:
jQuery.validator.addMethod("selectnic", function(value, element){
return !/^[0-9]{9}[vVxX]$/.test(value)
}, "wrong nic number");
你的逻辑是倒退的;当验证通过时,您定义的方法应返回true
,当验证失败时,应返回false
。还请注意,在使用反转布尔值之后,可以直接返回测试()的结果。
代码>。试试这个:
jQuery.validator.addMethod("selectnic", function(value, element){
return !/^[0-9]{9}[vVxX]$/.test(value)
}, "wrong nic number");
非常感谢。但我仍然看不出这条消息在这里运行良好:单击按钮查看它的运行情况。你确定你的正则表达式正确吗?啊,我看到你的例子了。但是你说的“你的正则表达式”是什么意思?你说的“你确定你的正则表达式”是什么意思?我把完整的脚本放在这里谢谢你。但我仍然看不出这条消息在这里运行良好:单击按钮查看它的运行情况。你确定你的正则表达式正确吗?啊,我看到你的例子了。但是你说的“你的正则表达式”是什么意思?你说的“你确定你的正则表达式”是什么意思