Question

I have this application that uses jquery validation. (Been stopped from using Data Annotations :()

On submit and blur, i want to check for a duplicate value. Since this is my first time encounter with jquery validation (other than data annotation), i am not sure what is the best way here. There is a remote call in action here as well. I am thinking about some thing like following. Is this the correct way to do it?

$("#" + enums.RegisterUsername).rules("add", {
            onkeyup: false,
            **onfocusout/onblur: true, ?????????**
            required: true,
            minlength: 6,
            alphanumericwithbasicpunc: true,
            remote: SignUp.DuplicateUserIdCheckUrl,
            messages: {
                required: "<span style='color:red'>&nbsp;&nbsp;Required</span>",
                minlength: "<span style='color:red'>User name must be at least 6 characters in length.</span>",
                alphanumericwithbasicpunc: "<span style='color:red'>User name cannot contain the following characters: &,\, /, #, <, or >.</span>",
                remote: "<span style='color:red'>User name already taken by another user.</span>"
            }
        });

Thanks

Was it helpful?

Solution

You absolutely cannot put any of the .validate() method options inside of the .rules() method. Only rules (and messages) can go inside of this.

$("#" + enums.RegisterUsername).rules("add", {
      required: true,
      minlength: 6,
      alphanumericwithbasicpunc: true,
      remote: SignUp.DuplicateUserIdCheckUrl,
      messages: {
          required: "<span style='color:red'>&nbsp;&nbsp;Required</span>",
          minlength: "<span style='color:red'>User name must be at least 6 characters in length.</span>",
          alphanumericwithbasicpunc: "<span style='color:red'>User name cannot contain the following characters: &,\, /, #, <, or >.</span>",
          remote: "<span style='color:red'>User name already taken by another user.</span>"
      }
});

Also, in every case, onfocusout can never be set to true. Validation on blur is already the default behavior so setting this option to true can break the plugin. It can only be set to false if you want to disable it, or set to a function to over-ride it. If you want validation to occur on blur, the onfocusout option needs to be left out of .validate().

See the documentation: http://jqueryvalidation.org/validate/

OTHER TIPS

I have commented and tested the following. It is working on blur.

$("#" + enums.RegisterUsername).rules("add", {
            //onkeyup: false,
            //onfocusout: true,
            required: true,
            minlength: 6,
            alphanumericwithbasicpunc: true,
            remote: enums.DuplicateUserIdCheckUrl,
            messages: {
                required: "<span style='color:red'>&nbsp;&nbsp;Required</span>",
                minlength: "<span style='color:red'>&nbsp;&nbsp;User name must be at least 6 characters in length.</span>",
                alphanumericwithbasicpunc: "<span style='color:red'>&nbsp;&nbsp;User name cannot contain the following characters: &,\, /, #, <, or >.</span>",
                remote: "<span style='color:red'>&nbsp;&nbsp;User name already taken by another user.</span>"
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top