4

I need to validate input field, it should be integer(0-100), if max_seat = 5 then min_seat should be less than or equal to max_seats.

<form class="cmxform form-horizontal" id="table_info" name="table_info"  method="post" action="">                                                       
<input class="span6 " id="max_seat"  name="default_seats" type="text" />
<input class="span6 " id="min_seats"  name="default_seats" type="text" />
</form>

and the jquery is looking like this, how to create rules in this type validation, thank you

    $("#table_info").validate(
    {
        rules: {
            min_range :"required integer",
            max_seats:"required integer"
        },
        messages: {
            max_seats: "* Required",
            min_range: "* Required",
        }
    });
3
  • 2
    You need to write a custom validation method that compares the value in one field to another field. Commented Jul 27, 2014 at 14:23
  • yes, but i dont know where to do those validation thats why i asked this question bro Commented Jul 27, 2014 at 14:34
  • 1
    Use $.validator.addMethod() Commented Jul 27, 2014 at 14:37

1 Answer 1

8

This is not how you declare multiple rules. (This shorthand style can only be used with one rule.)

rules: {
    min_range :"required integer",
    max_seats:"required integer"
}

You must declare each rule separately. And for an integer, you can use the digits method.

Your HTML is also not valid for this plugin...

<input class="span6 " id="max_seat"  name="default_seats" type="text" />
<input class="span6 " id="min_seats"  name="default_seats" type="text" />

Each field must contain a unique name attribute and you would declare your rules based on that name. (Declare the messages option similarly)

rules: {
    min_seats: { // <- this is the NAME attribute of the field, not ID
        required: true,
        digit: true
    },
    max_seats: { // <- this is the NAME attribute of the field, not ID
        required: true,
        digit: true
    }
}

To compare the two fields, you must use the .addMethod() method and declare your new custom rule the same as above.

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.