Skip to content

Instantly share code, notes, and snippets.

@molovo
Created April 7, 2013 13:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save molovo/5330415 to your computer and use it in GitHub Desktop.
Some yummy jQuery date validation. Was designed to allow a standard text input to accept date values, which could then be recognised by a datepicker widget which is only able to recognise inputted values in dd/mm/yyyy format. Could also be used to simply allow the user to use their preferred format, but still have consistency within the database…
$('input.class').blur(function() {
// First grab the date entered
var enteredDate = $(this).val();
// If enteredDate is only a number, add slashes at the correct points
if ( enteredDate == parseFloat(enteredDate) ) {
enteredDate = enteredDate.replace(/(\S{2})/g,"$1/");
enteredDate = enteredDate.replace(/\/$/,""); // removes the final slash
//alert (enteredDate);
}
// Replace dashes and dots with slashes for consistency
enteredDate = enteredDate.replace(/[-|.]/g, '/');
// If a two digit year is entered, prefix 19 for > 31, and 20 for < 32
var parts = enteredDate.split("/");
if (parts[parts.length - 1].length == 2) {
if (parts[parts.length - 1] < 32) {
// Make sure only last value of parts[parts.length - 1] is replaced (e.g. if 12/12/12 is entered only the year is replaced)
var positionOfYear = enteredDate.lastIndexOf(parts[parts.length - 1]);
enteredDate = enteredDate.substring(0,positionOfYear) + '20' + parts[parts.length - 1];
} else if (parts[parts.length - 1] > 31) {
// Make sure only last value of parts[parts.length - 1] is replaced (e.g. if 12/12/12 is entered only the year is replaced)
var positionOfYear = enteredDate.lastIndexOf(parts[parts.length - 1]);
enteredDate = enteredDate.substring(0,positionOfYear) + '19' + parts[parts.length - 1];
}
} else {
// Remove third slash if four digit year is entered
if ( enteredDate.lastIndexOf("/") > 6 ) {
enteredDate = enteredDate.replace(/\/([^\/]*)$/,'$1');
}
}
// Split array into d/m/y variables
var parts = enteredDate.split("/");
var d = parts[0];
var m = parts[1];
var y = parts[2];
// Validate enteredDate against Georgian Calendar
if ( m>12 || m<01 || d<01 ||
( d>30
&& ( m==09 || m==04 || m==06 || m==11 )
)
|| ( d>29 && m==02 && Math.ceil(y/4) == Math.floor(y/4) )
|| ( d>28 && m==02 && Math.ceil(y/4) !== Math.floor(y/4) )
|| d>31 )
{
// Date is invalid, enter your error callback here
} else {
// Date is valid. Final checks to add leading zeroes to single digit days and months
if ( d.length < 2 ) {
enteredDate = '0' + d + '/' + m + '/' + y;
}
if ( m.length < 2 ) {
enteredDate = d + '/' + '0' + m + '/' + y;
}
if ( d.length < 2 && m.length < 2 ) {
enteredDate = '0' + d + '/' + '0' + m + '/' + y;
}
// Update field with final validated date
$(this).val(enteredDate);
}
}).blur();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment