简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM