1

I'm trying to set up a jQuery Validate dependency-expression so that users need to either use the text field OR upload a file, but this code is not working: both fields are always required. I'm guessing <input type="file"> isn't supported by #id:empty, but what can I use in its place?

Here's a jsFiddle.

Javascript:

jQuery(document).ready(function() {
    jQuery("#form1iienom").validate({
        errorClass:"errorlabels",
        rules: {
            form1apptext: {
                required: "#form1upload:empty"
            },
            form1upload: {
                required: "#form1apptext:empty",
                extension: "jpg|jpeg|pdf|png|doc|docx"
            },
        },
        messages: {
            form1apptext: "You must either enter a reason for your nomination or upload a file.",
            form1upload: {
                required: "You must either enter a reason for your nomination or upload a file.",
            },
        }
    });
});

HTML:

<form name="form1iienom" id="form1iienom" method="post" action="/applications/iie/review" enctype="multipart/form-data">
<textarea name="form1apptext" id="form1apptext"></textarea><br />
<br />
<em>(allowed file types are jpg, jpeg, pdf, png, doc, and docx; maximum file size is 3 MB)</em>
<input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
<input type="file" name="form1upload" id="form1upload" /><br />
<input type="submit" value="Submit" />
</form>

1 Answer 1

1

As per documentation, the proper selector is :blank, not :empty.

Also see the example, as per documentation, using the depends sub-rule...

rules: {
    contact: {
        required: true,
        email: {
            depends: function(element) {
                return $("#contactform_email:checked")
            }
        }
    }
}
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.