简体   繁体   中英

Custom jquery validation rule not triggered

I have created a custom validation rule, named 'uniqueUserName'. I use it to validate input field username_ID in my form. The problem is that it isn't triggered. I can see in Firebug that no request is sent to the server, which will happen when the rule is triggered.

The form which I want to validate is dynamically loaded into a div.

My scripts:

$(document).ready(function() {
         //First. load the form and add it to the div. 
         $.get("registeraccount.php", function(data){
             $("#adminarea").html(data);
                         //addValMeth();
                         attacheValidation("#registerform");

        });

     //function addValMeth(){
       //Add custom validator rule
       $.validator.addMethod("uniqueUserName", function(value, element) {

            $.get("databasescripts/validate_username.php", {username: value}, function(data2){

                   if(data2 == "1"){
                       return true ;                   

                   }else{
                       return false;
                   }
                });
       }, "Användarnamnet är inte unikt");
      //};

                $("#registerform").live("submit", function(e){
                        //Prevent the form from submitting normally
                        e.preventDefault();

                    $.post("registeraccount.php",$(this).serialize(),function(msg){
            });
    });

   function attacheValidation($form){ 
                    //Define validation rules
                    $($form).validate({ 
                            rules: { 
                              firstname: "required",// simple rule, converted to {required:true} 
                              surname: "required",
                              //phonenumber: "required",
                              address: "required",
                             // address2: "required",
                              zipcode: "required",
                              //username_ID: "return true;",
                              city: "required",

                              email: {// compound rule 
                                required: true, 
                                email: true 
                              }, 
                                  username_ID: {
                                  required: true,
                                  uniqueUserName: true
                              }
                            }, 
                            messages: { 
                              firstname: "Förnamn saknas" ,
                              surname: "Efternamn saknas",
                              address: "Adress saknas",
                              zipcode: "Postkod saknas",
                              city: "Ort saknas",
                              companyname: "Företagsnamn saknas",
                              email: "Korrekt emailadress saknas"                             
                            } 
                          }); 
                    };

  });

My form:

<form id='registerform' action='registeraccount.php' method='post'> 
            Ägare av konto</br>
            * Förnamn: <input type='text' name='firstname' /> <br/>
            * Efternamn: <input type='text' name='surname' /> <br/>
            * Email: <input type='text' name='email' /> <br/>
            * Telefon: <input type='text' name='phonenumber' /> <br/>
            Företagsinformation: </br>
            * Adress: <input type='text' name='address' /> <br/>
            Adress: <input type='text' name='address2' /> <br/>
            * Postnummer: <input type='text' name='zipcode' /> <br/>
            * Ort: <input type='text' name='city' /> <br/>

            <br/>
            Kontoinformation</br> 
            <div id='username_message'></div>
            * Användarnamn: <input id='username_ID' type='text' name='username' />

            <br/>
            Företagsinformation<br/>
            * Företagsnamn (som kunden kommer se det): <input type='text' name='companyname' /> <br/>
            Organisationsnummer: <input type='text' name='organisationnumber' /><br/>
            <input id='submitaccountregistration' type='submit' value='Submit Comment' /> 
        </form>

As you can see, I have tried loading the new validation method when the form has been loaded, calling a function (addValMeth). No difference. I did comment that functionality out without any different behaviour.

What is the reason for the custom rule not being triggered and how do I solve it? All other rules are working properly.

The problem is that I referenced the id of my input field. Bad choice appearently, it's the "name" attribute that should be used.

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