0

Why doesn't it work to define classrules like documented in the jquery.validator documentation?

Jsfiddle

My javascript

jQuery.validator.addClassRules('foo', {
    required: true,
    digits: true
});
jQuery('#test').validate({
    rules: {
        field: 'foo'
    }
});

My html

<form id="test">
    <input type="text">
</form>
1
  • 1
    Think about the confusion of future readers who see an accepted answer telling you to add a name attribute to an element that already contains a name attribute. If you don't want to accept the technically correct answer, at least post your own and accept that instead. Thank-you. Commented Mar 13, 2013 at 21:37

2 Answers 2

2

Your input fields MUST have the name attribute to work with the Validate plugin. It's missing in your fiddle.

<input id="2" name="numberVal" class="numberVal" type="text" />

jsFiddle

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

5 Comments

I don't get your point. You have my fiddle and code above, could you edit it as stated in your post?
I attached the wrong fiddle, sorry. This is my markup, where shall I add a name attribute? There is just one input field? <input name="field" type="text">
@junior, check your jsFiddle. It does not match your OP... which one do you want fixed?
I totally messed up the post. The correct fiddle is updated in my post now.
@junior, this answer is correct but also has nothing to do with the code in your question and jsFiddle. The future reader is going to be really lost since there's already a name attribute in the OP.
1

Ignoring your original jsFiddle, this answer simply answers the question with the code as it's posted.

The code in your OP, using the addClassRules method, requires that you use the class attribute within the input element...

HTML:

<form id="test">
    <input name="field" type="text" class="foo" />
    <input type="submit" />
</form>

jQuery:

jQuery.validator.addClassRules('foo', {
    required: true,
    digits: true
});

jQuery('#test').validate({
    // your other options
});

DEMO: http://jsfiddle.net/G4rb3/1/

Comments

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.