Click to See Complete Forum and Search --> : Javascript alternate to IsDate?


Mark Collins
12-02-2003, 04:33 AM
Right I will get down to business straight away, for the past 14 hours I have been searching for a JavaScript alternate to ASP/VBscript IsDate function.
Unfortunately my search has been nearly completely fruitless; I have found many hundreds of posts concerning “date validation” i.e. checking a date is within a valid range, or that a date does not conflict with another date, checking formatting etc…

All I am looking to do is add a nice little JavaScript onto my ASP page that checks 2 separate date’s from dropdowns on the currant ASP page and make sure the dates are VALID i.e. real dates, "02/31/2003" is an example of a date that does not exist.
And either onSbutmit or after entry (from different select box's) if the date is not valid an alert box would display a message along the lines of "Sorry the dates you have submitted are incorrect, please insert valid dates and re-submit this form".

-------

If anyone here is not aware of what the isDate function in ASP/VBscript is ill give a basic quick definition.

IsDate returns a Variant subtype (Boolean) that indicates weather an expression can be converted into / is a valid date.



<%@LANGUAGE="VBSCRIPT" CODEPAGE="CP_ACP"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<%
If Isdate(Request.Form("DateMonth") & "/" & Request.Form("DateDay") & "/" & Request.Form("DateCurrantYear") Then
Response.write “Success valid dates at last!”
End If
%>
</body>
</html>



I’ve looked about many large forums and JavaScript websites but unfortunately a lot of JavaScript websites out there only have JavaScript’s for you too rip rather than articles explaining content and / or discussing problems within JavaScript and solutions (most likely I’ve just been looking in all of the wrong places).
All I’ve come up with is this;



<SCRIPT LANGUAGE="JavaScript">
function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat);

if (matchArray == null) {
alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
return false;
}

month = matchArray[1];
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) {
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}

if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true;
}
</script>


Now with my knowledge of JavaScript (which is minimal) I have absolutely no idea if this will work properly, could someone please help my by checking is this script does indeed work for the intention I have in mind and also, please if you can bear looking through this post anymore and still have energy to type give me a working example of how this function could be called?

Many thanks and sorry for the huge and probably grammatically incorrect post

Mark Collins

Pittimann
12-02-2003, 05:16 AM
Hi!

First of all: there is nothing wrong with the function (including leap year validation etc.).
Here is an example how to use it:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<SCRIPT LANGUAGE="JavaScript">
function isDate(dateStr) {
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) {
alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
return false;
}
month = matchArray[1];
day = matchArray[3];
year = matchArray[5];
if (month < 1 || month > 12) {
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true;
}
</script>
</head>
<body>
<form name="myForm" method="post" action="YourActionHere" onSubmit="return isDate(document.myForm.date.value)">
<input type="text" name = "date"><br>
<input type="submit" value="check date">
</body>
</html>

Cheers - Pit

Mark Collins
12-02-2003, 05:33 AM
Brilliant!

But with that yet another problem arises, would I be able to adapt this script for use with dropdown boxs rather than a text box.

i.e.


<%@LANGUAGE="VBSCRIPT" CODEPAGE="CP_ACP"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<Table>
<TR>
<TD HEIGHT="100" VALIGN="Bottom">
<Select name="DateDay">
<%
For VarCounter = 1 to 31
%>
<option value="<%= VarCounter %>"><%= VarCounter %></option>
<%
Next
%>
</Select>
<Select Name="DateMonth">
<%
For VarCounter = 1 to 12
%>
<option value="<%= VarCounter %>"><%= MonthName(VarCounter) %></option>
<%
Next
%>
</Select>
<Select Name="DateCurrantYear">
<%
For VarCounter = 0 to 5
%>
<option value="<%= (Year(Date)) + VarCounter %>"><%= (Year(Date)) + VarCounter %></option>
<%
Next
%>
</Select>
</TD>
</TR>
</Table>
</body>
</html>


Would there be a way to incluse an adapted version of the script to check from the dropdown rather than a textbox? And if it is indeed possible would it be possible for one of you to please show me how / explain to me where i should go to learn how to code this.

Many Thanks

Mark Collins

Pittimann
12-02-2003, 05:39 AM
Hi!

Just hold on a few minutes or return back later.

I will deal with it...

Cheers - Pit

Mark Collins
12-02-2003, 05:56 AM
Pittimann - for you and indeed all the poeple that moderate / post on these forums I thank you, it's very rare for a "newbie" javascript programmer to get any recognition more often than not im told to RTFM or something along the lines, hopefully with abit of studying and support from the rest of the guys here ill be a happy little javascript bunny in a few months.

Pittimann
12-02-2003, 06:27 AM
Hi Mark!

It took me a while - sorry (had a phone call in the meantime).
Here it is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<SCRIPT LANGUAGE="JavaScript">
var monthName=new Array(11);
monthName[0]="January";
monthName[1]="February";
monthName[2]="March";
monthName[3]="April";
monthName[4]="May";
monthName[5]="June";
monthName[6]="July";
monthName[7]="August";
monthName[8]="September";
monthName[9]="October";
monthName[10]="November";
monthName[11]="December";
function isDate() {
month = document.myForm.DateMonth.value;
day = document.myForm.DateDay.value;
year = document.myForm.DateYear.value;
if ((month==3 || month==5 || month==8 || month==10) && day==31) {
alert(monthName[document.myForm.DateMonth.selectedIndex]+" doesn't have 31 days!")
document.myForm.DateDay.focus;
return false;
}
if (month == 1) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
document.myForm.DateDay.focus;
return false;
}
}
return true;
}
</script>
</head>
<body>
<form name="myForm" method="post" action="YourActionHere" onSubmit="return isDate()">
<Select name="DateDay">
<script language="JavaScript" type="text/javascript">
<!--
for (var i=1; i<32;i++){
if (i<10) i2='0'+i;
else i2=i;
document.write('<option value="'+i+'">'+i2+'</option>');
}
document.write('</Select><Select Name="DateMonth">');
for (var i=0; i<12;i++){
document.write('<option value="'+i+'" name="monthName">'+monthName[i]+'</option>');
}
document.write('</Select><Select Name="DateYear">');
var now = new Date()
Year1=now.getYear();
document.write('<option value="'+Year1+'">'+Year1+'</option>');
for (var i=1; i<5;i++){
document.write('<option value="'+(Year1+i)+'">'+(Year1+i)+'</option>');
}
document.write('</Select>');
//-->
</script>
<input type="submit" value="check date">
</body>
</html>

Hope that helps you...

Cheers - Pit

Mark Collins
12-02-2003, 07:43 AM
Exactly what I wanted, Brilliant.

Thank you so much this problem was niggling at me for awhile and now its out the way I can code with a clear concience again :D