开发者

JQuery Validate and database calls

I am using Jörn Zaefferer's JQuery Validate, but I need to make some database calls to validate some fields (for example to check if a User Name is unique). Is this possible using this plugin, and if so, does someone have some syntax examples? Here is my current code :

$("form").validate({
           rules: {
                txtUserName: {
                   required: true,
      开发者_如何学Go             minlength: 4
               },
               txtPassword: {
                   required: true
               },
               txtConfirmPassword: {
                   required: true,
                   equalTo: "#txtPassword"
               },
               txtEmailAddress: {
                required: true,
                email: true
               },
               txtTelephoneNumber: {
                   required: true,
                   number: true
               }
           },
           messages: {
                txtUserName: {
                    required: "Please enter a User Name",
                    minlength: "User Name must be at least 4 characters"   
               },
               txtPassword: {
                   required: "Please enter a Password"
               },
               txtConfirmPassword: {
                   required: "Please confirm Password",
                   equalTo: "Confirm Password must match Password"
               },
               txtEmailAddress: {
                   required: "Please enter an Email Address",
                   email: "Please enter a valid Email Address"
               },
               txtTelephoneNumber: {
                   required: "Please enter a Telephone Number",
                   number: "Telephone Number must be numeric"
               }
           }
       });
   });

EDIT :

I have got this far, but when I do this, I lose the values on my form, presumably because the form has already posted at this point?

$("form").validate({
     //errorLabelContainer: $("#divErrors"),

         rules: {
             txtUserName: {
                 required: true,
                 minlength: 4
             },
             txtPassword: {
                 required: true
             },
             txtConfirmPassword: {
                 required: true,
                 equalTo: "#txtPassword"
             },
             txtEmailAddress: {
                 required: true,
                 email: true
             },
             txtTelephoneNumber: {
                 required: true,
                 number: true//,
                 //postalCode:true
             }
         },
         messages: {
             txtUserName: {
                 required: "Please enter a User Name",
                 minlength: "User Name must be at least 4 characters"
             },
             txtPassword: {
                 required: "Please enter a Password"
             },
             txtConfirmPassword: {
                 required: "Please confirm Password",
                 equalTo: "Confirm Password must match Password"
             },
             txtEmailAddress: {
                 required: "Please enter an Email Address",
                 email: "Please enter a valid Email Address"
             },
             txtTelephoneNumber: {
                 required: "Please enter a Telephone Number",
                 number: "Telephone Number must be numeric"
             }
         },
         onValid: addUser()
     });
 });

function addUser() {

   alert($('input[name="txtUserName"]').val());

}


There is a remote rule which you use. You can specify a POST URL which can then check the data and return true/false. Take a look at its documentation for more details. It's exactly for your scenario


JQuery validation plugin performs validation on every change and focusout of the input. I don't think it's good idea to call web service and check database for every single change of focusout of field. Instead, you could do it via ajax on form submit for example.


You can use an ajax call to retrieve data from database and then using those data you can process validation with you remote rules.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜