Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date validation

Posted on 2001-07-03
11
Medium Priority
?
258 Views
Last Modified: 2012-05-04
Hi.

I have 3 drop down boxes.One contains values for day(1-31),the 2nd one for month and the 3rd for year.

How do I validate that all these boxes contains values other than those set on default?Also how do I validate that there won't be any unusual dates like 29th February(in the case of a non leap year) or 31st of June???Also I need to check for leap year.

Can anyone help?Thanks.
0
Comment
Question by:segafreak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 4

Expert Comment

by:lokeshv
ID: 6248035
0
 
LVL 1

Expert Comment

by:asere
ID: 6248813
function ValidDate(y, m, d) {
     var mDate = new Date(y, m, d);
     if (y != mDate.getFullYear() || m != mDate.getMonth() || d != mDate.getDate())) {return false;} else {return true;};
};
0
 
LVL 3

Expert Comment

by:dapperry
ID: 6248923
Instead of having the three drop downs, you might want to consider having one text box, and a small window that opens on button click that will allow the user to select the date via a calendar type control. If you want to go down this road, I can provide code for you.

:) dapperry
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:segafreak
ID: 6250998
Ok,I think I'll post my current code down.I don't know why the validateDate function is not working correctly...

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Add Upcoming Events</title>
<script language="Javascript">
<!--
function validateFormTH(f)
{

      var eDay = document.addEvents.day[document.addEvents.day.selectedIndex].value;
      var eMonth = document.addEvents.month[document.addEvents.month.selectedIndex].value;
      var eYear = document.addEvents.year[document.addEvents.year.selectedIndex].value;
      
      if(document.addEvents.day.selectedIndex == 0)                        
      {
            alert("Please select a day to continue")
            return false
      }
      else if(document.addEvents.month.selectedIndex == 0)
      {
            alert("Please select a month to continue")
            return false
      }
      else if(document.addEvents.year.selectedIndex == 0)
      {
            alert("Please select a year to continue")
            return false
      }
      else if(validateDate(eDay, eMonth, eYear))
      {
                  return true            
      }
      else
      {
            return false
      }
      
}

function validateDate(day, month, year)
{
      
//30 days month
      if((month == "4") || (month == "6") || (month == "9") || (month == "11"))
      {
            if(day == "31")
            {

                  alert("There is only 30 days in the month of " + month + ".")
                  return false;
            }
      
      }
            
      else if(month == "2")
      {
            if(year % 4 == 0)//leap year
            {
                  if((day == "30") || (day == "31"))
                  {
                        alert("There is only 29 days in the month of february.")
                        return false;
                  }
            }
            
            else if(year % 4 != 0)
            {
                  if((day == "29") || (day == "30") || (day == "31"))
                  {
                        alert("There is only 28 days in the month of february.")
                        return false;
                  }
            }
      }
      
            return true;
}

function validateNum(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!")
field.focus();
field.select();
   }
}


//--->
</script>
</head>

<body bgcolor="#336666">
<div align="center">
<form name="addEvents" action="a_addedEvent_Successful.asp" method="POST" onsubmit="return validateFormTH(this.form)">
<p align="center">&nbsp;</p>
<p align="center"><font size="5" color="#FFFFFF">Add Upcoming Events</font></p>
<hr size=1 width="50%">
<br>
<br>
<table>
<tr><td><font color="#FFFFFF">Type of Events&nbsp;</font> </td><td><font color="#FFFFFF">:</font></td>
<td> <select name="eventType">
<option selected>Events</option>
<option value="Course">Course</option>
<option value="Seminair">Talk</option>
<option value="Gathering">Gathering</option>
</select></td></tr>
<tr><td><font color="#FFFFFF">Name of Event&nbsp;</font> </td> <td><font color="#FFFFFF">:</font></td><td><input type="text" name="eventName" size="30"></td></tr>
<tr><td><font color="#FFFFFF">Date</font> </td><td><font color="#FFFFFF">:</font></td>
<td><select name="day">
<option selected>Day</option>
<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">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="month">
<option selected>Month</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="year">
<option selected>Year</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
</td></tr>
<tr><td><font color="#FFFFFF">Venue</font> </td><td><font color="#FFFFFF">:</font></td><td><input type="text" name="venue" size="25"> </td></tr>
<tr><td><font color="#FFFFFF">Fees</font></td><td><font color="#FFFFFF">: S$</font></td><td><input type="text" name="fee" size="10" onBlur="validateNum(this)"></td></tr>
<tr><td><font color="#FFFFFF">Duration</font></td><td><font color="#FFFFFF">:</font></td><td><input type="text" name="duration" size="10"> </td></tr>
<tr><td><font color="#FFFFFF">Description</font> </td><td><font color="#FFFFFF">:</font></td><td><textarea name="desc" rows="4" cols="23">
</textarea></td></tr>
</table>
<br>

<input type="submit" value="Add Event">&nbsp<input button type="reset" value="Reset" name="reset">
</div>
</form>

</body>

</html>
0
 
LVL 1

Expert Comment

by:balanagu
ID: 6251757
try this, i did the corrections in validatedate() and month list box.

-----------------

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Add Upcoming Events</title>
<script language="Javascript">
<!--
function validateFormTH(f)
{

     var eDay = document.addEvents.day[document.addEvents.day.selectedIndex].value;
     var eMonth = document.addEvents.month[document.addEvents.month.selectedIndex].value;
     var eYear = document.addEvents.year[document.addEvents.year.selectedIndex].value;
     
     if(document.addEvents.day.selectedIndex == 0)                    
     {
          alert("Please select a day to continue")
          return false
     }
     else if(document.addEvents.month.selectedIndex == 0)
     {
          alert("Please select a month to continue")
          return false
     }
     else if(document.addEvents.year.selectedIndex == 0)
     {
          alert("Please select a year to continue")
          return false
     }
     else if(validateDate(eDay, eMonth, eYear))
     {
        alert("valid date")
        return true          
     }
     else
     {
                              alert("invalid date");
          return false
     }
}

function validateDate(day, month, year)
{
      var objDt=new Date(year,month,day)
      if(objDt.getDate()==day && objDt.getMonth()==month && objDt.getYear()==year)
      {
            return true;
      }
      else
      {
            return false;
      }
}

function validateNum(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!")
field.focus();
field.select();
  }
}


//--->
</script>
</head>
<body bgcolor="#336666">
<div align="center">
<form name="addEvents" action="a_addedEvent_Successful.asp" method="POST" onsubmit="return validateFormTH(this.form)">
<p align="center">&nbsp;</p>
<p align="center"><font size="5" color="#FFFFFF">Add Upcoming Events</font></p>
<hr size=1 width="50%">
<br>
<br>
<table>
<tr><td><font color="#FFFFFF">Type of Events&nbsp;</font> </td><td><font color="#FFFFFF">:</font></td>
<td> <select name="eventType">
<option selected>Events</option>
<option value="Course">Course</option>
<option value="Seminair">Talk</option>
<option value="Gathering">Gathering</option>
</select></td></tr>
<tr><td><font color="#FFFFFF">Name of Event&nbsp;</font> </td> <td><font color="#FFFFFF">:</font></td><td><input
type="text" name="eventName" size="30"></td></tr>
<tr><td><font color="#FFFFFF">Date</font> </td><td><font color="#FFFFFF">:</font></td>
<td><select name="day">
<option selected>Day</option>
<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">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="month">
<option selected>Month</option>
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
<option value="3">April</option>
<option value="4">May</option>
<option value="5">June</option>
<option value="6">July</option>
<option value="7">August</option>
<option value="8">September</option>
<option value="9">October</option>
<option value="10">November</option>
<option value="11">December</option>
</select>
<select name="year">
<option selected>Year</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
</td></tr>
<tr><td><font color="#FFFFFF">Venue</font> </td><td><font color="#FFFFFF">:</font></td><td><input type="text"
name="venue" size="25"> </td></tr>
<tr><td><font color="#FFFFFF">Fees</font></td><td><font color="#FFFFFF">: S$</font></td><td><input type="text"
name="fee" size="10" onBlur="validateNum(this)"></td></tr>
<tr><td><font color="#FFFFFF">Duration</font></td><td><font color="#FFFFFF">:</font></td><td><input
type="text" name="duration" size="10"> </td></tr>
<tr><td><font color="#FFFFFF">Description</font> </td><td><font color="#FFFFFF">:</font></td><td><textarea
name="desc" rows="4" cols="23">
</textarea></td></tr>
</table>
<br>

<input type="submit" value="Add Event">&nbsp<input button type="reset" value="Reset" name="reset">
</div>
</form>

</body>

</html>

0
 
LVL 1

Expert Comment

by:balanagu
ID: 6251772
you can also change your numerical validation function as,

function validateNum(field) {
     var numReg= /[^\d]/
     if (numReg.test(field.value)
     {
          alert("Invalid entry!  Only numbers are accepted!")
          field.focus();
          field.select();
     }
}


balanagu
0
 

Accepted Solution

by:
bvjagan earned 60 total points
ID: 6251848
Hi seqafreak,

   Try this..

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Add Upcoming Events</title>
<script language="Javascript">
<!--
function validateFormTH(f)
{

     var eDay = document.addEvents.day[document.addEvents.day.selectedIndex].value;
     var eMonth = document.addEvents.month[document.addEvents.month.selectedIndex].value;
     var eYear = document.addEvents.year[document.addEvents.year.selectedIndex].value;
     //alert(eDay);
     //alert(eMonth);
     //alert(eYear);
     if(document.addEvents.day.selectedIndex == 0)
     {
          alert("Please select a day to continue")
          return false
     }
     else if(document.addEvents.month.selectedIndex == 0)
     {
          alert("Please select a month to continue")
          return false
     }
     else if(document.addEvents.year.selectedIndex == 0)
     {
          alert("Please select a year to continue")
          return false
     }
     else if(validateDate(eDay, eMonth, eYear))
     {
               return true
     }
     else
     {
          return false
     }

}

function validateDate(day1, month1, year1)
{
//alert(day1);
//alert(month1);
//alert(year1);
if (isNaN(day1) ||isNaN(month1)||isNaN(year1))
      {
             alert("Invalid Date");

            return false;
      }

var day = parseInt(day1);
      var month = parseInt(month1);
      var year = parseInt(year1);


      if((year<1900)||( year >9999))
      {
             alert("Invalid Date");

            return false;
      }

      if((month>12)||(month < 1))
      {
            alert('Invalid Date');

            return false;
      }


      if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
      {
            if((day>31)||(day<1))
            {
                  alert('Invalid Date');

                  return false;
            }
      }
      else if((month==4)||(month==6)||(month==9)||(month==11))
      {
            if((day>30)||(day<1))
            {
                  alert('Invalid Date');

                  return false;
            }
      }
      else
      {
            if((year%4)==0)
            {
                  if((day>29)||(day<1))
                  {
                        alert('Invalid Date');

                        return false;
                  }
            }
            else
            {
                  if((day>28)||(day<1))
                  {
                        alert('Invalid Date');

                        return false;
                  }
            }
      }
      return true;
}

function validateNum(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!")

field.select();
  }
}


//--->
</script>
</head>

<body bgcolor="#336666">
<div align="center">
<form name="addEvents" action="#" method="POST" onsubmit="return validateFormTH(this.form)">
<p align="center">&nbsp;</p>
<p align="center"><font size="5" color="#FFFFFF">Add Upcoming Events</font></p>
<hr size=1 width="50%">
<br>
<br>
<table>
<tr><td><font color="#FFFFFF">Type of Events&nbsp;</font> </td><td><font color="#FFFFFF">:</font></td>
<td> <select name="eventType">
<option selected>Events</option>
<option value="Course">Course</option>
<option value="Seminair">Talk</option>
<option value="Gathering">Gathering</option>
</select></td></tr>
<tr><td><font color="#FFFFFF">Name of Event&nbsp;</font> </td> <td><font color="#FFFFFF">:</font></td><td><input
type="text" name="eventName" size="30"></td></tr>
<tr><td><font color="#FFFFFF">Date</font> </td><td><font color="#FFFFFF">:</font></td>
<td><select name="day">
<option selected>Day</option>
<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">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="month">
<option selected>Month</option>
<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">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="year">
<option selected>Year</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
</select>
</td></tr>
<tr><td><font color="#FFFFFF">Venue</font> </td><td><font color="#FFFFFF">:</font></td><td><input type="text"
name="venue" size="25"> </td></tr>
<tr><td><font color="#FFFFFF">Fees</font></td><td><font color="#FFFFFF">: S$</font></td><td><input type="text"
name="fee" size="10" onBlur="validateNum(this)"></td></tr>
<tr><td><font color="#FFFFFF">Duration</font></td><td><font color="#FFFFFF">:</font></td><td><input
type="text" name="duration" size="10"> </td></tr>
<tr><td><font color="#FFFFFF">Description</font> </td><td><font color="#FFFFFF">:</font></td><td><textarea
name="desc" rows="4" cols="23">
</textarea></td></tr>
</table>
<br>

<input type="submit" value="Add Event">&nbsp<input button type="reset" value="Reset" name="reset">
</div>
</form>

</body>

</html>
0
 

Author Comment

by:segafreak
ID: 6251864
erm,the date should not only validate today's date,it should validate furthur dates.
0
 

Author Comment

by:segafreak
ID: 6251878
Finally,someone posts up the solution I want!!!Thanks a lot man!
0
 
LVL 1

Expert Comment

by:balanagu
ID: 6251914
poor fellow, i gave a better validation with less coding.

balanagu
0
 
LVL 1

Expert Comment

by:jswalia
ID: 7111930
you can try the combination of JavaScript with VBScript:

<Script Language='Javascript'>
 function validDate()
 {
   var month, day, year;
   month = form1.cboMonth.value;
   day = form1.cboDay.value;
   year = form1.cboYear.value;
   if !checkDate(m,d,y)
   {
    alert('Not a valid Date');
   }
 }
</Script>

<Script Language='VBScript'>
 function checkDate(m,d,y)
   dim dt
   dt = m & "/" & d & "/" & y
   checkDate = isdate(dt)
 end function
</Script>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question