Validating a range of dates

Discussion in 'JavaScript' started by trevlar, Nov 7, 2006.

  1. #1
    Hi all,

    I'm trying to validate a range of dates such that the start date must be before the end date.

    If the user tries to view results from, say 11/25/06 to 11/12/06, it will produce an alert when he hits Submit.

    The dates are in MM/DD/YYYY format.

    Any ideas?

    Thanks!
     
    trevlar, Nov 7, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Give this a try.

    
    <script type="text/javascript">
    <!--
    var date1 = '11/25/06';
    var date2 = '11/12/06';
    
    var x = date1.split('/');
    var y = date2.split('/');
    
    var str1 = x[2] +''+ x[0] +''+ x[1];
    var str2 = y[2] +''+ y[0] +''+ y[1];
    
    if (str1 > str2)
    {
    	alert('Invalid date');
    }
    else
    {
    	alert('OK');
    }
    //-->
    </script>
    
    Code (javascript):
     
    nico_swd, Nov 8, 2006 IP
  3. trevlar

    trevlar Peon

    Messages:
    65
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Works great! Thanks.
     
    trevlar, Nov 8, 2006 IP