2

I am unable to get custom error messages working for the jQuery validation plugin. I'm using jQuery 1.7.2 and jquery validation plugin 1.9.0.

My form is simple:

<form id="registrationform">
  <label for="rid">User Id</label><br/>
  <input id="rid" type="text" class="required"/><br/>
  <label for="remail">Email</label><br/>
  <input id="remail" type="text" class="required email"/>
</form>

My javascript is simple:

$(document).ready(function() {
    $("#registrationform").validate({
        rules: {
            rid : {
                required : true
            },
            remail: {
                required : true,
                email : true
            }
        },
        messages: {
            rid : {
                required : "id can not be empty"
            },
            remail: {
                required: "email can not be empty",
                email : "enter a valid email"
            }
        }
    });
});
2
  • I'm not 100% sure the rules: {} are specified correctly. In reality, since you already have the class='required email', entries in your HTML, you shouldn't need any special rules. I'd try removing the rules section, clearing your browser cache, and trying again. Commented Jun 5, 2012 at 21:28
  • JMC, I tried it without the rules first. That didn't solve it. Sam Tyson's solution, adding name attributes, solved the problem. Commented Jun 6, 2012 at 14:07

1 Answer 1

13

As crazy as it sounds, this is resolved by adding the name attribute to each of the input controls.

<form id="registrationform">
  <label for="rid">User Id</label><br/>
  <input id="rid" name="rid" type="text" class="required"/><br/>
  <label for="remail">Email</label><br/>
  <input id="remail" name="remail" type="text" class="required email"/>
</form>

Here is my jsFiddle to prove it out.

Sign up to request clarification or add additional context in comments.

3 Comments

It looks like the idOrName() function in validate() is stumbling over the lack of the name attribute or confusing the checkable() result.
Sam, thanks so much. I've confirmed your solution in both my isolated test case and in my app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.