noclist
noclist

Reputation: 1819

jQuery Validate - Multiple Rules per Element?

I have a form which I would like to validate certain fields as being required as well as being digits. I'm having trouble figuring out how to apply both rules to said fields.

Currently here is how I am applying only the required rule, displaying the message in a span I have built into my form.

    if (jQuery.validator) {
    jQuery.validator.setDefaults({
        errorPlacement: function (error, element) {
            error.appendTo('#invalid-' + element.attr('id'));
        }
    });
}

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


<label for="AvgGroupSize">Average Group Size:</label><br />
<span id="invalid-AvgGroupSize"></span>
<input type='text' id='AvgGroupSize' class='required' size='4' value='15'/> EEA's

How would I able to add a second validation rule (digits) to this field?

Thank you.

Upvotes: 1

Views: 1628

Answers (1)

RAS
RAS

Reputation: 3385

You just need to add a class to your input:
<input type='text' id='AvgGroupSize' class='required digits' size='4' value='15'/> EEA's

if digits validation class doesn't exist yet you need to add i through: Add Class Rules

Another option might be is just adding rules to your input.

something like this:

$("#AvgGroupSize").rules("add", {
 minlength: 2
});

Read more on how to Add rule

Upvotes: 1

Related Questions