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.
1 Answer 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...
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...
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.
4 Comments
123
passes validation but abc
does not. This may be worthy of a bug report to the plugin's author.
Date
constructor will validate.