Skip to content

Instantly share code, notes, and snippets.

@webdood
Created May 18, 2010 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdood/405402 to your computer and use it in GitHub Desktop.
Save webdood/405402 to your computer and use it in GitHub Desktop.
var DATE_REGEX = /^(\d{2})-(\d{2})-(\d{4})$/
////////////////////////////////////////////////////////////////////////////////
// //
// validateDate([object|string] oHTMLInputElement) - Validates a date value //
// =============================================== //
// You can pass in the id to an <input> Element, an actual //
// oHTMLInputElement or an actual date value in the form of a string //
// //
////////////////////////////////////////////////////////////////////////////////
function validateDate(oHTMLInputElementOrDateValue) {
var theDateValueToValidate = "";
var theValidatedDateValue = false;
var bIsInputElement = false;
if (typeof(oHTMLInputElementOrDateValue)=="string") {
var oHTMLInputElement = document.getElementById(oHTMLInputElementOrDateValue);
if (oHTMLInputElement && typeof(oHTMLInputElement=="object") && (oHTMLInputElement.tagName=="INPUT")) {
theDateValueToValidate = oHTMLInputElement.value;
bIsInputElement = true;
} else {
// Presumably they just passed in a date string
theDateValueToValidate = oHTMLInputElementOrDateValue;
bIsInputElement = false;
}
} else {
// They have passed in an Object
if (typeof(oHTMLInputElementOrDateValue=="object") && (oHTMLInputElementOrDateValue.tagName=="INPUT")) {
var oHTMLInputElement = oHTMLInputElementOrDateValue;
theDateValueToValidate = oHTMLInputElement.value;
bIsInputElement = true;
}
}
// If we have a date value to actually validate, let's get started
if (theDateValueToValidate != "") {
var theDateValue = theDateValueToValidate.replace(/\//g,"-"); // Ruby-side only accepts hyphens
if (DATE_REGEX.test(theDateValue)) {
theValidatedDateValue = theDateValue;
} else {
if (theDateValue.indexOf('-') > -1) {
var theMonth = theDateValue.split('-')[0];
var theDay = theDateValue.split('-')[1];
var theYear = theDateValue.split('-')[2];
theMonth = parseInt(theMonth).PadWithZero(); // If they've entered a single digit for either Day or Month, pad with zero accordingly
theDay = parseInt(theDay).PadWithZero();
if (theYear.length==2) { theYear = "20" + theYear; } // If they've entered a two-digit year, assume it's in the 21st century
theDateValue = theMonth + "-" + theDay + "-" + theYear; // Rebuild the date. Hopefully this will work
if (DATE_REGEX.test(theDateValue)) {
theValidatedDateValue = theDateValue;
}
}
// If we get to this point and none of our efforts have worked to format the date as "required", throw an error (if they are using an INPUT element)
if (!theValidatedDateValue && bIsInputElement) {
alert("ERROR: Date must be formatted as follows:\n\n\tDD-MM-YYYY\n\nPlease correct before continuing");
oHTMLInputElement.focus();
}
}
}
if (bIsInputElement && theValidatedDateValue) {
oHTMLInputElement.value = theValidatedDateValue;
}
return theValidatedDateValue;
}
@webdood
Copy link
Author

webdood commented May 18, 2010

Here is a somewhat badass Date Validator routine I wrote today that takes in either a string date value or an Element Object and validates the date to be formatted as DD-MM-YYYY.

I wrote it for use by a Ruby on Rails application, but it's pretty handy as a general library function.

Shannon Norrell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment