I have a form with a jQuery UI Datepicker, which then gets validated using the jQuery Validation plugin.
Datepickers are created using:
$("#dob").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "dd/mm/yy",
onSelect: function (){
this.focus();
},
});
However the validation plugin seems to validate in the US format when I require it to validate using dd/mm/yyyy. This means that any date past the 12th of any month is deemed invalid.
I have looked around and I found a method to add a different way to validate the date using a regular expression:
$.validator.addMethod(
"australianDate",
function(value, element) {
return value.match(/^(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[012])\/((19|20)\\d\\d)$/);
},
"Please enter a date in the format dd/mm/yyyy."
);
which is then added to the rules:
dob:{
required:true,
australianDate: true
},
but this still seems to fail for any input.
the regular expression I am using is one I have used in the past with php and is a bit more accurate then the generic (any2numbers/any2numbers/any4numbers) expression, and I would like to be able to use it in the javascript aswell.
Do javascript regular expressions need to be in a different format to php? is that where I am going wrong..
Any help is greatly appreciated.
Just a comment:
A better date validation function is: