I need to validate my birth dates in my signup form a valid date must not be in the future, and no non-existent dates e.g 31 june is there an easy way to do this without coding for each month?
Asked
Active
Viewed 1,159 times
1
-
4jQuery Date Picker . or HTML5 `type="date"` – Tushar Gupta - curioustushar Apr 18 '14 at 08:20
3 Answers
0
If you are using jQuery datepicker you can use this code:
$("#born_date").datepicker({
changeYear: true,
changeMonth: true,
maxDate: '+0d',
yearRange: '-90:+0',
numberOfMonths: 1
});
Best regards!

paulalexandru
- 9,218
- 7
- 66
- 94

Borza Adrian
- 509
- 2
- 12
-
I prefer to use the old selectbox format and validate the form data than a plugin I just need the code to validate a selectbox with a day, month and year field – droo Apr 18 '14 at 14:14
0
If you want to do this manually.. This code should suffice (I'm pretty sure the pattern supports dd/mm/yyyy
).
function checkDateTime(newDate) {
var pattern = '^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$';
var regex = new RegEx(pattern);
if (typeof newDate === 'string' && newDate.trim().match(regex)) {
var parsedDate = new Date(newDate);
var today = new Date();
if (parsedDate.getTime() >= today.getTime()) {
//The date is valid...
}
}
}

Community
- 1
- 1

classicjonesynz
- 4,012
- 5
- 38
- 78
-
thank you for the answer I eventually coded the entire thing manually using javascript it was relatively easy since there are only 7 months that end on the 31st in a year – droo Apr 25 '14 at 03:32
0
Use Javascript in combination with html5 validation pattern:
<input type="text" pattern="(0[1-9]|1[0-9]|2[0-9]|3[01])/(0[1-9]|1[012])/[0-9]{4}"
onchange="this.className = Date.parse(this.value) < Date.now() && 'valid'">
Find more patterns here: http://html5pattern.com/Dates
If someone

Christian Landgren
- 13,127
- 6
- 35
- 31