1

I'm using the jQuery Validate plugin to validate a form date. I've set the rules up for the input element to validate as required and as a date. The only rule though that seems to be firing is the required one as I can put any thing in (text, numbers etc) and it passes validation. Only if the input field is blank does it show an error.

Here's my script:

 $(function() {
 $('#form1').validate({
 rules: {
 entryDate: {
 required: true,
 date: true
 }
 },
 messages: {
 entryDate:{
 required: "A Date is required",
 date: "Please enter a valid date"
 }
 }
 });
 });

which is inside a script tag.

Here's my test form:

 <form action="" method="POST" id="form1">
 <input id="entryDate" name="entryDate" class="date" type="text" />
 <input type="submit" name="submit" id="submit" />
 </form>

Can anyone point me in the right direction please? Why are non dates being accepted?

I've included the scripts in the file (jQuery and Validate) and both are loading ok.

Sparky
98.8k26 gold badges202 silver badges293 bronze badges
asked Jan 29, 2013 at 3:33
2
  • 2
    This is explained in the documentation: Return true, if the value is a valid date. Uses JavaScripts built-in Date to test if the date is valid, and does therefore no sanity checks. Only the format must be valid, not the actual date, eg 30/30/2008 is a valid date. Works with text inputs! This means that anaything that doesn't return "Invalid Date" when passed to the Date constructor will validate. Commented Jan 29, 2013 at 3:46
  • As test input I tried "abc" and "123" both of these entries appear to have passed validation. Based on comment is this expected and normal behaviour for a date validation? Commented Jan 29, 2013 at 4:00

1 Answer 1

1

Quote OP: "Why are non dates being accepted?"

Quote OP comment: "As test input I tried 'abc' and '123' both of these entries appear to have passed validation. Based on comment is this expected and normal behaviour for a date validation?"

Your code, exactly as you've posted it, is working as expected. As you can see, abc and 123 do not pass validation...

http://jsfiddle.net/zsCM3/

You also do not need to use inline class="date" when you've already declared date: true within the rules option. Although this has no bearing on the issue, demo is working the same in both cases...

http://jsfiddle.net/TWEXJ/


EDIT:

Confirmed that in Chrome (24.0.1312.52), an entry of 123 is indeed passing validation. I tried the jsFiddle with both jQuery 1.8.3 and 1.9.0.

answered Jan 29, 2013 at 4:21
Sign up to request clarification or add additional context in comments.

4 Comments

Hi - thanks just reviewed - thank you. However in CHrome the script as still accepts 123 as a valid date. I retested it in firefox and everything works well and tested as expected. I know chrome only validated us dates but didnt know this could be a potential issue with chrome as well. Thank you
@Ray, Since the posted code is working, can you please clarify your question?
@sparky- sorry always forget not to press the return key when adding a comment as it sends to early - completed comment as above. Appears a browser issue - Not sure how to resolve from here so will accept it works everywhere else and move on...thank you.
@Ray... that's really odd. In my Chrome, 123 passes validation but abc does not. This may be worthy of a bug report to the plugin's author.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.