Solved

date validation

Posted on 2003-10-22
5
257 Views
Last Modified: 2012-06-27
I have a text field that takes date in the format MM/DD/YYYY. i did the validation for format. also it should make sure that entered date is after todays date or systems date.
how can i do that using javascript?

ALso i have two text fields to indicate the range of period like from_______ to _________

how can i validate that second date is alwas after first one??
0
Comment
Question by:soft_techy
  • 4
5 Comments
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9603077
here's a function I used to find the difference in date

function dateDiff(strDate1,strDate2)
{
  datDate1= Date.parse(strDate1);
  datDate2= Date.parse(strDate2);
  return((datDate2-datDate1)/(24*60*60*1000));
}

this function should be used before the the date is validate to be valid
0
 
LVL 12

Accepted Solution

by:
kingsfan76 earned 150 total points
ID: 9603305
here's a function I just wrote that will check if a date enter is valid for dates with MM/DD/YYYY format.  give it a try and see if it works for you  (of course you need to modify it slightly)

<script>
function CheckDate()
{
    var strDate = new String(myform.TheDate.value);
    var theDate = new Date(strDate);
    var today = new Date();
    if(isNaN(theDate))
    {
         alert("Date is invalid")
         return false;
     }
    else
    {
         var strTheDate = theDate.toString();
         var arrOriginalDate = strDate.split("/");
         theMonth = theDate.getMonth() + 1;
          if(theMonth != parseInt(arrOriginalDate[0]))
         {
              alert("Date is invalid");
              return false;
          }
       }
      
}
</script>

<form name="myform">
<input name="TheDate"><input type=button value="Check" onClick="CheckDate()">
</form>
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9603310
by combining the dateDiff() function with the CheckDate() function you should be able to accomplish what you need.

if you need help combining them to do what you need, please ask and i'll be glad to help.  :-)
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9603320
p.s. by combining i didn't mean to make those 2 functions into one.  I mean to write another function to call these two functions to work together, like:

call checkDate() to verify date is valid
if valid
call dateDiff to check if one date is greater than another (pass in current date as parameter if you need to check the date entered is greater than today's date)
0
 
LVL 1

Author Comment

by:soft_techy
ID: 9603758
It worked for me Thanks
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

810 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