PDA

View Full Version : Date Validation


balan
03-08-2006, 04:55 AM
Hello,
I have a text field with a date picker near to it. I want the user to pick a date. Like ( Date Between 12-03-2006 and 04-09-2006 ). If the user chooses 11-03-2006 or 05-09-2006 an alert message should be displayed. Pls help.

Balen

Jon Hanlon
03-08-2006, 06:45 PM
Need a leeetle bit more information.
What format are you inputting the date?
MM/DD/YYYY DD/MM/YYYY DD-MMM-YYYY YYYY-MM-DD?

balan
03-09-2006, 01:16 AM
Hello,
I am asking the user to choose a date between Jan 01 to Jan 10. The user picks his date using the date picker. If the user chooses a date which is not between the jan1 to jan10 it should display an alert message saying "pls choose the date within the specified range". I am accepting the date format as - DD/MM/YYYY

Thanks
balen

Need a leeetle bit more information.
What format are you inputting the date?
MM/DD/YYYY DD/MM/YYYY DD-MMM-YYYY YYYY-MM-DD?

Jon Hanlon
03-09-2006, 05:56 PM
function verifyDate(sValue) { // returns false or a Date object.
var theDate = stringToDate(sValue); // your textbox value
if (!theDate) {
alert("Please enter a valid Date");
return false;
}

var minDate = new Date("Sat, 1 Jan 2006");
var maxDate = new Date("Wed, 1 Mar 2006");

if (theDate.getTime() < minDate.getTime()) {
alert("Please enter a date on or after " + minDate);
return false;
}
if (theDate.getTime() > maxDate.getTime()) {
alert("Please enter a date on or before " + maxDate);
return false;
}
return theDate;
}

function stringToDate(inStr) { // returns a date from input dd/mm/yyyy
if (!inStr) return false;
var reDate = /^\s*(\d{1,2})\D*(\d{1,2})\D*(\d{2,4})\s*$/
var matchArray = reDate.exec(inStr);
if (!matchArray) return false;
var tDay = parseInt(matchArray[1],10);
var tMonth = parseInt(matchArray[2],10);
var tYear = parseInt(matchArray[3],10);
if (isNaN(tDay + tMonth + tYear)) return false;
if (tYear.toString().length == 3) return false;
if (tYear.toString().length <= 2)
tYear += (tYear < 50) ? 2000 : 1900;
return isValidDate(tYear, tMonth - 1, tDay);
}

function isValidDate(inY,inM,inD) {
var testDate = new Date(inY, inM, inD);
if (!testDate) return false;
return ( testDate.getDate() == inD
&& testDate.getMonth() == inM
&& testDate.getFullYear() == inY) ? testDate : false;
}

balan
03-10-2006, 06:28 AM
Hi jon,
Your coding works fine. Thank you for helping me out to solve this problem.

Balen