codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
This 17 message thread spans 2 pages: [1]  2  > >  
  IsDate() in JavaScript?  Archive Import (bluewater68) at 09:49 on Monday, April 14, 2003
 

Hello everyone.
Is there a function in JavaScript like "IsDate()" to validate data like date in form?
In VbScript it will be something like this "if not IsDate(x) then..."
Thank you all.

  Re: IsDate() in JavaScript?  Archive Import (charley) at 10:21 on Monday, April 14, 2003
 

Funny - I was looking for just this too!There isn`t an inbuild one I don`t think. I came across this one on the web and it seems to be fine.. hope it helps..


// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(dateStr) {

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

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

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (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) { // check for february 29th
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; // date is valid
}


  Re: IsDate() in JavaScript?  Archive Import (chao) at 21:06 on Sunday, June 01, 2003
 

Thank you very mach!

  Re: IsDate() in JavaScript?  Archive Import (nessan) at 15:57 on Tuesday, July 08, 2003
 

Thank a lot for the function, is great...
I add a line to code: document.yourform.yourdatefield.focus();
thus the user cannot input a invalid date....

  Re: IsDate() in JavaScript?  Yusairi at 00:35 on Wednesday, July 09, 2003
 

try this one :

<HTML><HEAD>
<SCRIPT>
function getYear(d) {
return (d < 1000) ? d + 1900 : d;
}

function isDate (year, month, day) {
// month argument must be in the range 1 - 12
month = month - 1; // javascript month range : 0- 11
var tempDate = new Date(year,month,day);
if ( (getYear(tempDate.getYear()) == year) &&
(month == tempDate.getMonth()) &&
(day == tempDate.getDate()) )
return true;
else
return false
}

if (isDate(1997, 2, 28))
alert("1997-02-31 is valid!");
else
alert("1997-02-31 is invalid!");
</SCRIPT></HEAD><BODY>test date</BODY>
<HTML>

Yusairi: site expert
http://www.codetoad.com


  Re: IsDate() in JavaScript?  damienos at 20:48 on Wednesday, December 31, 2003
 

That script needed some work, here is that same script with some additions that will prevent errors.
you need this declaration outside of the function.
Declare it globally:

var datemsg="";
Incorporate this variable to perform any alerts.

function IsDate(dateStr)

{
//Modified by DO 12/31/2003
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?

var datestatus=true;

datemsg="";



if (matchArray == null || matchArray[1]==null)

{

datemsg="-----> Please enter date as mm/dd/yyyy " + "\n";

return false;

}

else

{

if(matchArray[3]=null || matchArray[5]==null)

{

datemsg="-----> Please enter date as mm/dd/yyyy " + "\n";

return false;

}

}



month = matchArray[1]; // p@rse date into variables



day = matchArray[3];



year = matchArray[5];



if (month < 1 || month > 12)

{ // check month range

datemsg=datemsg + "-----> Month must be between 1 and 12." + "\n";

datestatus=false;

}



if (day < 1 || day > 31)

{

datemsg=datemsg + "-----> Day must be between 1 and 31." + "\n";

datestatus=false;

}



if ((month==4 || month==6 || month==9 || month==11) && day==31)

{

datemsg=datemsg + "-----> Month " + month + " doesn`t have 31 days!" + "\n";

datestatus=false;

}



if (month == 2)

{ // check for february 29th

var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

if (day > 29 || (day==29 && !isleap))

{

datemsg=datemsg + "-----> February " + year + " doesn`t have " + day + " days!" + "\n";

datestatus=false;

}

}

return datestatus;

}


  Re: IsDate() in JavaScript?  damienos at 20:56 on Wednesday, December 31, 2003
 

heres a better copy:

function IsDate(dateStr)

{
//Modified by DO 12/31/2003
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
var datestatus=true;
datemsg="";

if (matchArray == null || matchArray[1]==null)
{
datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
return false;
}
else
{
if(matchArray[3]=null || matchArray[5]==null)
{
datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
return false;
}
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12)
{ // check month range
datemsg=datemsg + "----- Month must be between 1 and 12." + "\n";
datestatus=false;
}

if (day < 1 || day > 31)
{
datemsg=datemsg + "----- Day must be between 1 and 31." + "\n";
datestatus=false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
datemsg=datemsg + "----- Month " + month + " doesn`t have 31 days!" + "\n";
datestatus=false;
}

if (month == 2)
{ // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap))
{
datemsg=datemsg + "----- February " + year + " doesn`t have " + day + " days!" + "\n";
datestatus=false;
}
}
return datestatus;
}


  Re: IsDate() in JavaScript?  damienos at 20:59 on Wednesday, December 31, 2003
 

<code>
function IsDate(dateStr)

{
//Modified by DO 12/31/2003
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?
var datestatus=true;
datemsg="";

if (matchArray == null || matchArray[1]==null)
{
datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
return false;
}
else
{
if(matchArray[3]=null || matchArray[5]==null)
{
datemsg="----- Please enter date as mm/dd/yyyy " + "\n";
return false;
}
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12)
{ // check month range
datemsg=datemsg + "----- Month must be between 1 and 12." + "\n";
datestatus=false;
}

if (day < 1 || day > 31)
{
datemsg=datemsg + "----- Day must be between 1 and 31." + "\n";
datestatus=false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
datemsg=datemsg + "----- Month " + month + " doesn`t have 31 days!" + "\n";
datestatus=false;
}

if (month == 2)
{ // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap))
{
datemsg=datemsg + "----- February " + year + " doesn`t have " + day + " days!" + "\n";
datestatus=false;
}
}
return datestatus;
}
</code>

  Re: IsDate() in JavaScript?  mikr0s at 12:35 on Wednesday, March 31, 2004
 

function myIsDate(mystring)
{//--BOF
var mystring, myresult ;
var mystring = new Date(mystring);
isNaN(mystring)? myresult=false : myresult=true ;
return myresult ;
//--EOF
}

HTH - Hope This Helps

<Added>

a quick way for evaluating strings
needs some work for complete date evaluation

  Re: IsDate() in JavaScript?  joe2 at 12:45 on Thursday, October 28, 2004
 

Hi

i am getting day, month and year from drop dwon .user will select the date.
var check_date=hyear+","+hmonth+","+hday;

if (isDate(check_date)){
alert(" Valid ");
alert(check_date);
}
else
{
alert(" In-Valid ");
alert(check_date);
}

hyear, hmonth , hday are name of the drop down for date selection.
but it is always returning in-valid date.
What am doing wrong.
Please advice.
Thanks in advance.

  Re: IsDate() in JavaScript?  damienos at 17:12 on Thursday, October 28, 2004
 

It looks like your problem is here:
var check_date=hyear+","+hmonth+","+hday;

First hyear,hmonth and hday would have to be set as the id of of each drop down not just the name. The name is used for the server side post to request the value of the input(s) in the form, the id is used for client side script manipulation.

The above code should look more like this:

var x = document.all;
var cMonth=x.getElementByID("hmonth").value;
var cDay=x.getElementByID("hday").value;
var cYear=x.getElementByID("hyear").value;
var check_date=cMonth + "/index.html" + cDay + "/index.html" + cYear;

  Re: IsDate() in JavaScript?  joe2 at 07:54 on Friday, October 29, 2004
 

Thanks for your reply. Unfortunatly code is not working by getting
value by getElementId . Here is my code.

<script language="javascript">
<!--
function myvalue(){
function getYear(d) {
return (d < 1000) ? d + 1900 : d;
}

function isDate (year, month, day) {
// month argument must be in the range 1 - 12
month = month - 1; // javascript month range : 0- 11
var tempDate = new Date(year,month,day);
if ( (getYear(tempDate.getYear()) == year) &&
(month == tempDate.getMonth()) &&
(day == tempDate.getDate()) )
return true;
else
return false
}


var hday=document.getElementById('check_in_day').options[document.getElementById('check_in_day').selectedIndex].value;
var hmonth=document.getElementById('check_in_month').options[document.getElementById('check_in_month').selectedIndex].value;
var hyear=document.getElementById('check_in_year').options[document.getElementById('check_in_year').selectedIndex].value;
var check_in_date=hyear+", "+hmonth+", " + hday;


if (isDate(check_in_date)){
alert(" Valid ");
alert(check_in_date);
}
else
{
alert(" In-Valid ");
alert(check_in_date);
}

}
//-->
</script>
<form name="frmtest" onSubmit="return myvalue()">
<select name="check_in_day" id="check_in_day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29" selected>29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="check_in_month" id="check_in_month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10" selected>October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="check_in_year" id="check_in_year">
<option value="2004" selected>2004</option>
<option value="2005">2005</option>
</select>
<br>
<br>
<input type="submit" value="Submit">
</form>


What am i doing wrong. Please help.

Thanks



  Re: IsDate() in JavaScript?  damienos at 17:21 on Friday, October 29, 2004
 

The problem was that you were trying to pass a variable as arguments to your function and when you did that the function only saw one argument. You have to pass the arguments seperately. The code below works.

<script language="javascript">
<!--
function myvalue(){
function getYear(d) {
return (d < 1000) ? d + 1900 : d;
}

function isDate (year, month, day) {
// month argument must be in the range 1 - 12
month = month - 1; // javascript month range : 0- 11
var tempDate = new Date(year,month,day);
if ( (getYear(tempDate.getYear()) == year) &&
(month == tempDate.getMonth()) &&
(day == tempDate.getDate()) )
return true;
else
return false
}


var hday=document.getElementById('check_in_day').options[document.getElementById('check_in_day').selectedIndex].value;
//alert(hday);
var hmonth=document.getElementById('check_in_month').options[document.getElementById('check_in_month').selectedIndex].value;
//alert(hmonth);
var hyear=document.getElementById('check_in_year').options[document.getElementById('check_in_year').selectedIndex].value;
//alert(hyear);
//var check_in_date=hyear+", "+hmonth+", " + hday;
//alert(check_in_date)

if (isDate(hyear,hmonth,hday)){
alert(" Valid ");
//alert(check_in_date);
}
else
{
alert(" In-Valid ");
//alert(check_in_date);
}

}
//-->
</script>

<form name="frmtest" onSubmit="return myvalue()">
<select name="check_in_day" id="check_in_day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29" selected>29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="check_in_month" id="check_in_month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10" selected>October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="check_in_year" id="check_in_year">
<option value="2004" selected>2004</option>
<option value="2005">2005</option>
</select>
<br>
<br>
<input type="submit" value="Submit">
</form>


  Re: IsDate() in JavaScript?  Jai at 12:02 on Tuesday, January 09, 2007
 

afsdfsdf

  Re: IsDate() in JavaScript?  nyfrenchy at 16:56 on Thursday, March 22, 2007
 

the solution is very simple:

function isDate (value)
{
return (!isNaN (new Date (value).getYear () ) ) ;
}


This 17 message thread spans 2 pages: [1]  2  > >  







CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Random Password Generator.. problem in file handling
•  Re: Difference between two dates including From & To date
•  Re: dereferenceing a string to a constant name
•  Re: PHP swapping images
•  Re: JavaScript: Not working in Firefox 3.x
•  Re: Need Javascript Multiple sidebar menu with sliding effects
•  insert datepicker on each row
•  insert datepicker on each row
•  Microsoft JET Database Engine (0x80040E07) Data type mismatch in criteria expression.


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2010