1

I've a simple HTML input:

<input id="email" name="email" value="" type="email">

and validate it using JQuery validate:

$('form').validate({
    rules: {
      email: {
        required: 'true',
        email: 'true'
      }
    }
});

The rules are applied, I can see them in $('#email').rules(). However when I later run $('#email').valid() or $('form').valid() the input is marked as valid when empty, but invalid when an invalid email address is entered.

How do I ensure the input is invalid when empty?

Relevant JSfiddle: https://jsfiddle.net/ycypuy86/

3
  • 1
    Is this a simple typographical error though? I get that the actual fix itself was only a set of quotes, however the error wasn't made as a typo - it was made because of the unfamiliarity in difference between string and boolean. The fundamental idea behind that closure reason is because typos aren't useful to anyone in the future, but this specific case is likely very useful - I can see this being quite a common misconception. Commented Jun 9, 2017 at 16:02
  • I agree, I wouldn't consider it a typo. The confusion was exacerbated by the shorthand email: 'required' for email: {required: true}. Commented Jun 9, 2017 at 16:14
  • 2
    @Santi You may have a point, and thus I will remove the closing vote. I would rather invite future people reading this question to visit the official required method documentation before posting or looking for questions such as this. Commented Jun 9, 2017 at 16:27

1 Answer 1

4

I usually set these without quotes?

rules: {
      email: {
        required: true,
        email: true
      }
    }

remove quotes on your fiddle and it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Ya beat me to it, but I figured I could supply the updated JSFiddle I had to show that this change does in fact resolve OP's issue: jsfiddle.net/ycypuy86/3

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.