2011-01-07 10 views
25

Tôi đang cố gắng xác thực jQuery hoạt động trên trang web tôi đang tạo. Tôi có khoảng 6 lĩnh vực khác nhau có chứa các chi tiết của trang. Tôi đang sử dụng điều này khi tôi đang ẩn và cho họ thấy trải nghiệm người dùng tốt hơn.Xác thực jQuery onblur

Tôi có plugin hoạt động như tôi muốn bất cứ khi nào tôi thử gửi trang và bất cứ khi nào tôi thêm một ký tự (khi hộp văn bản yêu cầu 2 ký tự trở lên), tuy nhiên tôi cũng muốn xác thực xảy ra onblur. Tôi muốn thông báo cho người dùng của mình ngay lập tức nếu họ không đáp ứng điều kiện xác thực để họ có thể khắc phục ngay lập tức và không phải quay lại.

Bất cứ ai có thể cho tôi biết những gì tôi cần làm Tôi đang sử dụng plugin * http://bassistance.de/jquery-plugins/jquery-plugin-validation/.

này được mã jQuery Tôi đã viết cho đến nay:

 $("#aspnetForm").validate({ 
      rules: { 
      <%=txtFirstName.UniqueID %>: 
       { 
        required: true, 
        minlength: 2 
       } 
       , 
       <%=txtSurname.UniqueID %>: 
       { 
        required: true, 
        minlength: 2 
       } 
       , 
       <%=txtMobileNumber.UniqueID %>: 
       { 
        required: true, 
        minlength: 8 
       } 
       , 
       <%=Email.UniqueID %>: 
       { 
        required: true, 
        email: true 
       } 
       , 
        <%=ddDay.UniqueID %>: 
       { 
        required: true 

       } 
       , 
        <%=ddMonth.UniqueID %>: 
       { 
        required: true 

       } 
       , 
        <%=ddYear.UniqueID %>: 
       { 
        required: true 

       } 
       , 
       <%=txtHouseNumber.UniqueID %>: 
       { 
        required: true, 
        minlength:1 

       } 
       , 
       <%=txtAddress1.UniqueID %>: 
       { 
        required: true, 
        minlength:5 
       } 
       , 
       <%=txtCity.UniqueID %>: 
       { 
        required: true, 
        minlength:2 
       } 
       , 
       <%=txtSuburb.UniqueID %>: 
       { 
        required: true, 
        minlength:2 
       } 
       , 
       <%=txtPostCode.UniqueID %>: 
       { 
        required: true, 
        minlength:4, 
        maxlength:4 
       } 

       , 
       <%=UserName.UniqueID %>: 
       { 
        required: true, 
        minlength:4 

       } 
       , 
       <%=Password.UniqueID %>: 
       { 
        required: true, 
        minlength:4 

       } 
       , 
       <%=ConfirmPassword.UniqueID %>: 
       { 
        equalTo: "ctl00$ctl00$cpMain$cpLeft$Password" 

       } 
        , 
       <%=chkTerms.UniqueID %>: 
       { 
        required: true 


       } 

      }, 
      messages: { 
       <%=txtFirstName.UniqueID %>: 
      { 
       required: "Please enter your firstname", 
       minlength: "Minimum length is 2 characters" 
      }, 
       <%=txtSurname.UniqueID %>: 
      { 
       required: "Please enter your lastname", 
       minlength: "Minimum length is 2 characters" 
      }, 
      <%=txtMobileNumber.UniqueID %>: 
      { 
       required: "Please enter your mobile", 
       minlength: "Minimum length is 8 characters" 
      } 
      , 
      <%=ddDay.UniqueID %>: 
      { 
       required: "Please enter your date of birth" 

      } 
      , 
      <%=txtMobileNumber.UniqueID %>: 
      { 
        required: "Please enter your date of birth" 
      } 
      , 
      <%=txtMobileNumber.UniqueID %>: 
      { 
        required: "Please enter your date of birth" 
      } 
      , 
        <%=Email.UniqueID %>: 
        "Please enter a valid email" 
      , 
      <%=txtHouseNumber.UniqueID %>: 
      { 
        required: "Please enter your house number", 
        minlength:"Please add at least 1 character" 
      } 

      , 
      <%=txtAddress1.UniqueID %>: 
      { 
        required: "Please enter your address", 
        minlength:"Please add at least 5 characters" 
      } 
      , 
      <%=txtCity.UniqueID %>: 
      { 
        required: "Please enter your city", 
        minlength:"Please add at least 2 characters" 
      } 
      , 
      <%=txtSuburb.UniqueID %>: 
      { 
        required: "Please enter your city", 
        minlength:"Please add at least 2 characters" 
      } 
      , 
      <%=txtPostCode.UniqueID %>: 
      { 
        required: "Please enter your postcode", 
        minlength:"Please add the 4 required characters", 
        maxlength:"Only 4 characters are allowed" 
      } 
      , 
      <%=UserName.UniqueID %>: 
      { 
        required: "Please enter your username", 
        minlength: "Please add the 4 required characters" 

      } 
      , 
      <%=Password.UniqueID %>: 
      { 
        required: "Please enter your password", 
        minlength: "Please add the 4 required characters" 

      } 
      , 
      <%=ConfirmPassword.UniqueID %>: 
      { 
        equalTo: "Passwords must match" 
      } 
      , 
      <%=chkTerms.UniqueID %>: 
      { 
        required: "Please agree to the terms" 


      } 

      } 


     }); 


Bất cứ lời khuyên?

Trả lời

11

Tôi không thể thấy bất kỳ điều gì trong các tài liệu có thể thực hiện điều đó. Cách duy nhất tôi có thể nghĩ là làm nó.

$('#field1, #field2, #field3').blur(function(){ 
    validator.validate() 
}); 
+0

Hi Timothy, Cool nên tôi sẽ không điên. validator là gì? bạn có nghĩa là $ ("# aspnetForm"). validate(); thay vì validator.validate() –

+0

yea cái đó. lol sai lầm của tôi. bạn có thể tạo một đối tượng validator (mặc dù bạn không có, và bạn không cần phải). –

+13

Nó có thể được thực hiện ....... chỉ cần thêm onfocusout này: function (element) {$ (element) .valid(); }, làm việc một điều trị –

50

Diver Dan đã đúng

$('form').validate({ 
    onfocusout: function (element) { 
     $(element).valid(); 
    }, 
    rules: { 
     name: 'required', 
     from: 'required' 

    }, 
    messages: { 
     name: 'Please enter your firstname', 
     from: 'Please enter where are you from' 
    } 
}); 
+6

Tôi phải thêm "onkeyup: false" để có được hành vi mong muốn –

+1

Thực ra, tốt hơn nên sử dụng cùng một phương thức được sử dụng trong plugin. Vì vậy ... 'this.element (element)' thay vì '$ (element) .valid()'. Xem: http://stackoverflow.com/a/16205614/594235 – Sparky

+0

Điều này mang lại cho Uncaught TypeError: validator.settings [eventType] .call không phải là một hàm –

7

Bạn cũng có thể sử dụng cuộc gọi yếu tố của validator.

$('form').validate({ 
     onfocusout: function(element) { 
      this.element(element); 
     }, 
     rules: { 
      name: 'required', 
      from: 'required' 

     }, 
     messages: { 
      name: 'Please enter your firstname', 
      from: 'Please enter where are you from' 
     } 
    }); 
2

Chỉ cần đặt trên onkeyup = false

$('form').validate({ 
    rules: { 
     name: 'required', 
     from: 'required' 

    }, 
     onkeyup: false 
     , 
    messages: { 
     name: 'Please enter your firstname', 
     from: 'Please enter where are you from' 
    } 
}); 
+2

** Đây thực sự là câu trả lời đúng! ** Giải quyết vấn đề của tôi nhanh chóng xác thực. Cảm ơn –

0

Thia code will not fire validation onkeyup, but on blur "lost focus" the validation will be fire, as will once the user starts to edit the field, validation message will disappear. find more interesting other customization on this ref: https://jqueryvalidation.org/category/plugin/

$('#frm').validate({ 
      onkeyup: false, 
      focusCleanup: true 
     });