View Single Post
Join Date: Aug 2008
Posts: 380
Reputation: langsor is an unknown quantity at this point 
Solved Threads: 33
langsor langsor is offline Offline
Posting Whiz

Re: problem with date validation

 
0
  #3
Aug 6th, 2008
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
Reply With Quote