Solved

date validation

Posted on 2003-10-22
5
252 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now