0

I have a series of inputs on my page with the same css class. I would like to add jqueryvalidation rules to them based on an attribute that I have set on their parent object. The rules get added fine but the max value that I am setting is wrong. Here is the .js

$('.countable input').each(function() {
    var input = $(this);
    var parent = input.closest('.countable');
    var dataMax = parseInt(parent.attr('data-max'));
    input.rules("add", {
        number: true,
        min: 0,
        max: dataMax,
        messages: {
            number: "Must be a number",
            max: "Must be less than or equal to " + dataMax
        }
    });
});

The problem I have is that every input that is assigned a max value ends up getting the max value of 6, which happens to be the last input that is assigned by this each function. If I step through the each, I can see that dataMax has the correct value when it is assigned, but every input gets the same data max assigned to it. Is there another way to do this?

3
  • 1
    You need to show some sample HTML markup. Does each input element contain a unique name attribute? Commented Mar 4, 2014 at 16:26
  • @Sparky, that was the problem, thanks! If you submit an answer I will mark it as correct. Commented Mar 4, 2014 at 16:30
  • Will do. Please add some sample HTML markup to your OP so that my answer makes more sense. Commented Mar 4, 2014 at 16:35

1 Answer 1

1

Every input element considered for validation must contain a unique name attribute. This is the way the plugin keeps track of the inputs.

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

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.