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
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