2010-10-08 3 views
14

Jak sprawdzić poprawność pola adresu e-mail za pomocą trzech reguł z trzema niestandardowymi wiadomościami w kontenerze div. tj.jquery sprawdzania poprawności z wieloma regułami

rules: { 
    email: { 
     validationRule: true, 
     email: true, 
     remote: '/ajax/emailDuplicationCheck.php' 
     } 
    } 

jeśli najpierw jeden fałszywy komunikat powinien być „reguły poprawności nie powiodło się”

jeśli druga fałszywa (nie) „wpisz adres e-mail”

jeśli trzecia (zdalne) nie powiodła się. wiadomość powinna brzmieć "Konto już istnieje w bazie danych".

Mogę dodać jedną wiadomość dla wszystkich reguł, ale chcę dostosować wiadomość zgodnie z zasadami.

Odpowiedz

11

Spróbuj tego:

$("#myForm").validate({ // Replace #myForm with the ID of your form 
    rules: { 
     email: { 
      required: true, 
      email: true, 
      remote: { 
       url: "/ajax/emailDuplicationCheck.php", 
       type: "post", 
       data: { email: function() { 
        return $("#email").val(); // Add #email ID to your email field 
       } 
      } 
     } 
    }, 
    messages: { 
     email: { 
      required: 'Email address is required', 
      email: 'Please enter a valid email address', 
      remote: 'This email address has already been used' 
     } 
    }, 
    errorPlacement: function(error) { 
     $("#response").html(error); 
    } 
}); 

Nadzieja to pomaga!

+0

nr nie działa. wyświetla tylko wiadomość w wiadomości e-mail: "wprowadź adres e-mail". Jak wspomniałem. Chcę wyświetlić msg w pojemniku div? – Developer

+0

Składnia jest poprawna i tak należy ją wykonać, więc problem musi być gdzie indziej. 'validationRule' wygląda jak niestandardowa metoda sprawdzania poprawności. Co dokładnie ma robić? Czy możesz edytować pytanie i dodać kod dla tej metody? –

+0

Metoda validationRule nic nie robi. to jest po prostu prawdziwy zwrot. dobrze, jeśli usunę tę linię. nawet jeśli nie działa z dwiema regułami. i jak mogę wyświetlić go w Divcontainer z id #response. Chcę wyświetlić wiadomość w pojemniku div – Developer

1

Możesz użyć niestandardowych reguł sprawdzania poprawności wraz z własnymi niestandardowymi komunikatami dla każdej reguły.

Dla uproszczenia, oto przykład jak potwierdzić wejście „username” z trzech metod niestandardowych walidacji (każda ze swoim własnym komunikat o błędzie):

// Add three custom methods first: 

$.validator.addMethod("nameCustom", function(value, element) { 
    return this.optional(element) || /^[a-zA-Z ]+/.test(value); 
}, 'Please use english letters only.'); 

$.validator.addMethod("noSpaceStart", function(value, element) { 
    return value.indexOf(" ") != 0; 
}, "Name cannot start with a space"); 

$.validator.addMethod("noSpaceEnd", function(value, element) { 
    return value.lastIndexOf(" ") != value.length - 1; 
}, "Name cannot end with a space"); 

$('#form_to_validate').validate({ 
    rules: { 
     username: { 
      // Add the custom validation methods to the username input 
      noSpaceStart: true, 
      noSpaceEnd: true, 
      nameCustom: true, 
      // required and maxlength are built-in methods in the plugin and are ready to be used 
      required: true, 
      maxlength: 50 
     } 
    } 
});