Solved

date validation

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

896 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

19 Experts available now in Live!

Get 1:1 Help Now