简体   繁体   中英

Custom jQuery validate rule not working correctly

I try to use a custom jQuery validation method, like this:

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"); 

This is the rule:

contactpersonen_functie: {
    selectnic: true,
    required: true
},

The HTML is like this:

<div class="contact-input-field">
    <input type="text" class="input-text span2" id="contactpersonen_functie" name="contactpersonen_functie"></input>
</div>

So the required works, but the selectnic doesn't work, can anyone tell me why this is?

Thank you

I have it now like this:

jQuery.validator.addMethod("selectnic", function(value, element){
    return !/^[0-9]{9}[vVxX]$/.test(value)
}, "wrong nic number");

在此输入图像描述

 <div class="contact-label span2">
              <label for="contactpersonen-functie">Functie</label>
              <div class="contact-input-field">
                <input type="text" class="input-text span2 contactpersonen_functie" id="contactpersonen_functie" name="contactpersonen_functie"></input>
              </div>
            </div>

this is the total script:

 <![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"
                                    }
                                });

Your logic is backwards; the method you define should return true when the validation passes, and false when it fails. Note also that you can return the result of the test() directly after inverting the boolean value with ! . Try this:

jQuery.validator.addMethod("selectnic", function(value, element){
    return !/^[0-9]{9}[vVxX]$/.test(value)
}, "wrong nic number"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM