Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Validation

Status
Not open for further replies.

Antzz

Programmer
Jan 17, 2001
298
US
Guys,

Do any of you know a way or any reserved function in Javascript to validate whether a date is valid or not? For eg : 02/30/2001 is not a valid date. Date formate preferble is mm/dd/yyyy but not necessary.
All help is appreciated.

Thanx
 
you'd have to make an assoicative array out of the months and how many days are in each (depending on leap year of course) and test against it...

to see if the date is in the correct format you can test like this:

/^\d\d\/\d\d\/\d{4}$/.test(somestr)

that will return true if the date looks like ##/##/#### jared@aauser.com
 
You probably got no response because this is a pretty extensive program in JS.

Here is some code that I found.

[tt]
function isValidDate(date2check){
// dgk-10/07/00
// determines if the date string passed represents a valid date.
// returns 0 if the date is valid
// returns 1 if the date is not in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
// m/d/ccyy & m-d-ccyy are also acceptable
// returns 2 if the date is not a legal date (i.e. 02/30/1999)
var retval = 0
var aMMDDCCYY
var dtest
// use a regular expression pattern match to determine if the date format is valid
if (/^(\d\d?-\d\d?-\d{4})|(\d\d?\/\d\d?\/\d{4})|(\d{8})$/.test(date2check)){
dtest = new Date(date2check);
if (/\//.test(date2check)){
aMMDDCCYY = date2check.split("/");
}else{
if (/-/.test(date2check)){
aMMDDCCYY = date2check.split("-");
}else{
aMMDDCCYY = Array(date2check.substr(0,2), date2check.substr(2,2), date2check.substr(4,4))
dtest = new Date(aMMDDCCYY[0] + "/" + aMMDDCCYY[1] + "/" + aMMDDCCYY[2]);
}
}
if (dtest.getMonth() + 1 != aMMDDCCYY[0] || dtest.getDate() != aMMDDCCYY[1] || dtest.getFullYear() != aMMDDCCYY[2]){
retval = 2
}
}else{
retval = 1
}
return retval
}


function StdDateFormat(strDate){
// dgk-10/07/00
// receives a date string in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
// returns a date string in the standard format: mm/dd/ccyy.
var retval = ""
var aMMDDCCYY
if (isValidDate(strDate) == 0){
if (/\//.test(strDate)){
retval = strDate;
}else{
if (/-/.test(strDate)){
aMMDDCCYY = strDate.split("-");
}else{
aMMDDCCYY = Array(strDate.substr(0,2), strDate.substr(2,2), strDate.substr(4,4));
}
retval = (aMMDDCCYY[0] + "/" + aMMDDCCYY[1] + "/" + aMMDDCCYY[2]);
}
}
return retval;
}
[/tt]

Hope this helps,

-Vic vic cherubini
malice365@hotmail.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Oh o........I wanted to know if there was any reserved function..u know kinda in-built....I know how to code it...am lazy since why code if we already have a built in function - know what I mean?
 
Thanks anyways.....will let u know if I had been successful
 
There is no built in function to do what you are asking for.

Sorry. The above example that I posted and the way Jared told you how to do it are your best shot.

-Vic vic cherubini
malice365@hotmail.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====
 
Thanks vic cherubini, for your post so long ago.

I was about to ask the forum for a robust way to validate date values. I'll have to give your post a try.

To the rest out there. I'm a newbie at REs... is there a good concise online resource that explains REs? I'm currently developing in IIS5.0 with WinServer2k running my pages with both ASP/VBscript and Javascript...

Earnie Eng
 
Simple strict format date field validation. You are limited to one format of date entry: dd/mm/yyyy. yy can be entered it will be altered to yyyy with the following rule: yy <= 10 is += 2000 / yy > 10 += 1900
Any non word delimeter can be entered but will be changed to "/"


[tt]
function isLeap (y) {
return (((y%4==0) && ((!(y%100==0)) || (y%400==0))) ? true : false );
}

function daysInMonth (m,y) {
mArr = new Array (31,28,31,30,31,30,31,31,30,31,30,31);
return (m==2 && isLeap(y)) ? 29 : mArr[m-1];
}

function fixYear (y) {
return (y <= 10 ) ? y + 2000 : (y < 100) ? y + 1900 : y;
}

function validDMY (d,m,y) {
return (y < 1650 || m < 0 || m > 12 || d < 0 || d > daysInMonth(m,y)) ? false : true;
}

function checkDate (f,nullOK) {
if (nullOK && f.value=='')
return true;
else {
var RE = /(\d{1,2})\W(\d{1,2})\W(\d{2,4})/.exec(f.value);
if (RE) {
if (validDMY(RE[1]*1, RE[2]*1, fixYear(RE[3]*1))) {
f.value = RE[1]*1 + "/" + RE[2]*1 + "/" + fixYear(RE[3]*1);
return true;
}
else {
setTimeout ('document.' + f.form.name + '.' + f.name + '.focus()',100);
setTimeout ('window.alert (\'Invalid date entered. Must be dd/mm/yyyy.\')', 100);
return false;
}
}
else {
setTimeout ('document.' + f.form.name + '.' + f.name + '.focus()',100);
setTimeout ('window.alert (\'Invalid date entered. Must be dd/mm/yyyy.\')', 100);
return false;
}
}
}


function validateForm (f){
if (checkDate(f.txtDate,true))
return true;
else
return false;
}

</script>


</HEAD>
<BODY>
<form name="frmSample" onSubmit="return validateForm(this);">
<p>Enter a Date: <input type="text" name="txtDate" maxlength="10" size="15" onChange="checkDate(this,true);">
<P><input type="submit" name="Submit" value="Submit">
</form>
[/tt]
 
Neo... how would I re-write that script to allow a less strict handling of the month and day?

so that it accepts:
m/d/yy
mm/d/yy
mm/dd/yy

where yy can be 2 or 4 digit...

Earnie Eng
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top