2011-07-20 17 views
14

Uwaga: poniższy kod służy tylko do demonstracji i używam jQuery i jQuery validate plugin.Jak ręcznie unieważnić pola po wywołaniu AJAX z jQuery validate

Załóżmy, że mam formularz z dwóch pól (e-mail i numer inwentarzowy):

<form action="/something" method="post" id="demoForm"> 
    Inventory No: <input type="text" class="required" name="inventory_no" /> 
    Email: <input type="text" class="required" name="email" /> 
    <input type='submit' value='submit' /> 
</form> 

Binding wtyczki do postaci:

jQuery(function(){ 
    jQuery('#demoForm').validate(); 
}); 

//handle user submit 

jQuery('#demoForm').bind('submit', function (e) { 
    e.preventDefault(); 

    if (jQuery(e.target).valid()) { 
     //form is valid submit request thru ajax 

     jQuery.ajax({ 
      /*more options here*/ 

      success: function (data) { 

       /* 
       Server process request and finds that, email is already in use 
       and inventory number is invalid 
       so it sends data in some format may pe JSon 
       with field names and message indicating errors 
       eg: Email:"Already in use" 
       InventoryNo: "Invalid Inventory No", 
       Now can i invalidate these two fields on form mannualy 
       and display the message received from server for each field 
       */ 
      } 
     }); 
    } 
}); 
+0

Czy kiedykolwiek znalazłeś rozwiązanie tego problemu? Dokładnie to właśnie próbuję zrobić w tej chwili. – Richard

+0

znalazłem rozwiązanie tego po prostu daj mi trochę czasu, dam ci znać –

Odpowiedz

31

Jeśli wiesz, które pole jest nieprawidłowa, można użyć tego funkcjonować. http://docs.jquery.com/Plugins/Validation/Validator/showErrors

var validator = $("#myshowErrors").validate(); 
validator.showErrors({ 
    "firstname": "I know that your firstname is Pete, Pete!" 
}); 

Gdzie firstname jest własnością nazwa danej dziedzinie; tj. name="firstname".

+0

Prawidłowa odpowiedź, dziękuję panu! (OP powinien to zaakceptować.) –

+16

To wyświetli błąd, ale formularz będzie nadal przesyłany – Lyon

Powiązane problemy