Solved

date validation

Posted on 2003-10-22
5
262 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
[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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

733 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