Dirty Bird Design
Dirty Bird Design

Reputation: 5553

jQuery validate depending on checkbox status

I have three fields in a form which is using jQuery validate.js:

<input type="text" id="StartDate" name="StartDate"><br />
<input type="text" id="EndDate" name="EndDate"><br />
<label for="AllDate"><input type="checkbox" name="AllDate" id="AllDate">All Dates</label>

I need #StartDate to be required on submit UNLESS #AllDate is checked. If #AllDate is checked, #StartDate is NOT required

Failed attempt(s):

rules: {
        StartDate: {
            required: {
                depends: function(element) {
                    return (! $("input[type='checkbox']:checked"))
                }
            }
        }
    }

I have had no luck creating a custom validation rule either. I am looking for a better way, one that works!

Upvotes: 0

Views: 460

Answers (2)

Steward Godwin Jornsen
Steward Godwin Jornsen

Reputation: 1179

Here is a custom one for you

if(($("#AllDate").is(':checked')) == true ){
    //Do what you want when all dates are checked

} elseif (($("#StartDate").val()) != "") {
    //Do what you want with StartDate

} else {
    //You have to enter a date

}

Typing on the fly, See if that helps you or gives you a basic idea

Upvotes: 0

dfsq
dfsq

Reputation: 193311

Try this:

return !$("#AllDate").is(':checked');

Upvotes: 4

Related Questions