0

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!

2 Answers 2

4

Try this:

return !$("#AllDate").is(':checked');
Sign up to request clarification or add additional context in comments.

2 Comments

I was trying to type out the whole function. Brevity FTW.
great, I came up with this required: "#AllDate:checked" but that obviously works exactly opposite of what I wanted. Greatly appreciated
0

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

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.