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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('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