0

I have the code that you can check in this fiddle: http://jsfiddle.net/4SCrp/5/

<form id="my_form">
    <input id="formfield1" type="text" maxlength="3" value="">
    <br/>
    <input id="formfield2" type="text" maxlength="3" value="">
    <br/>
    <input type="submit" value="submit">
</form>

and th followin script using jquery.validate plugin

$("#my_form").validate();

$('#formfield1').rules('add', {
    required: true,
    digits: true,
    max: 255
});

$('#formfield2').rules('add', {
    required: true,
    digits: true,
    max: 2
});

It is supposed to acept a maximum of 255 in the first field and a maximum of 2 in the second. But both give the error "Please enter a value less than or equal to 2." so it takes for all the last max defined.

Do you know how to solve that problem?

1 Answer 1

1

Its maxlength you need - not max check here -> http://docs.jquery.com/Plugins/Validation/Methods/maxlength#length

Try specifying the rules as follows :

$("#my_form").validate({
    rules: {
        formfield1: {
            required: true,
            digits: true,
            max: 255
        },
        formfield2: {
            required: true,
            digits: true,
            max: 2
        }
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

no, ´maxlenght´ will only check the number of characters, not if the value is bigger that a given figure. I want to check that the written value is less than a different figure on each field.
Edited my answer - sorry !! i actually read the question correctly this time !!
@David This works fine -> jsfiddle.net/4SCrp/7 - the submit button causes a problem but instant validation is working fine - note the name attribute on the form fields
I see. The key is that it makes the validation by the name attribute instead of by the id. But it could be cases when you may need several elements with the same name and different rules. For that I couldn't reach a solution. Thank you very much Manse!!
You can use the class if you have single rule for multiple elements
|

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.