943,686 Members | Top Members by Rank

Ad:
Aug 6th, 2008
0

problem with date validation

Expand Post »
Hello everyone, I am facing a problem with the start date and the end date.I have to do a validation such that the end date entered with the date picker should be equal to or greater than the start date.I have written the following code but it is not helping me out.Here is the code of javascript

<script language="javascript">

function validate()
{
if(document.tstest.timestamp1.value < document.tstest.timestamp.value)
{
alert('Please check the End date');
return false;
}
if(document.tstest.timestamp.value.length==0 && document.tstest.timestamp1.value.length==0) 
{
alert("Please enter the date");
return false;
}
return true;
}
</script>]
The html code is as follows. start date
<input readonly type="Text" id="start" name="timestamp" size = "20" value=""> End date
<input readonly type="Text" id="end" name="timestamp1" size = "20" value=""> The problem is that the validation is done only for days but not for the month and year.If you enter the start date as dd/mm/yyyy 12/8/2008 and End date as 11/8/2008 it will give an alert box but if you enter start date as 12/8/2008 and end date as 09/8/2008 than the form is submitted it is not validating. Please anyone can find a solution for this. thanks in advance.
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
shijunair is offline Offline
50 posts
since Jul 2008
Aug 6th, 2008
0

problem with date validation

Hello.
Is there no one who could solve my problem regarding the date validation.
I am still waiting for a reply from someone who could help me out of this.
Please give a response.
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
shijunair is offline Offline
50 posts
since Jul 2008
Aug 6th, 2008
0

Re: problem with date validation

You're trying to compare strings as if they were numbers ... that will give you unreliable results at best.

To compare as numbers you should do something like this ...

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2.  
  3. // grab and split date values
  4. var start = document.getElementById('start').value;
  5. var sarr = start.split('/');
  6. var sday = parseInt( sarr[0] );
  7. var smo = parseInt( sarr[1] );
  8. var syr = parseInt( sarr[2] );
  9.  
  10. var end = document.getElementById('end').value;
  11. var earr = end.split('/');
  12. var eday = parseInt( earr[0] );
  13. var emo = parseInt( earr[1] );
  14. var eyr = parseInt( earr[2] );
  15.  
  16. // here we must compare date values
  17. validate();
  18.  
  19. function validate () {
  20. switch ( true ) {
  21. case syr > eyr:
  22. alert( 'please pick an end year later than start year' );
  23. return false;
  24. case smo > emo:
  25. alert( 'please pick an end month later than start month' );
  26. return false;
  27. case sday > eday:
  28. alert( 'please pick an end day later than start day' );
  29. return false;
  30. default:
  31. document.getElementById('my_form_id').submit();
  32. }
  33. }
  34. </script>

I haven't actually tested the above script, so there might be some hidden (or not so hidden) hitch in it, but that type of thing is what you're looking for -- I believe.

Good luck
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Help with Drop down list
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Password encoding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC