Date Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    Date Validation

    Hi,

    How would i change this to check if the date in the field is at least 2 days in front of today's? Must work with this format yyyy-mm-dd

    Code:
     
    
    if (form.arrivaldate.value == "") {
        alert( "Please make sure your arrival date is at least 2 days in front of today's date" );
        form.arrivaldate.focus();
        return false ;
      }
    Cheers,
    Adam
  • scripto
    New Member
    • Oct 2006
    • 143

    #2
    I am assuming "in front of" means "before" today's date - yes?

    [CODE=javascript]if (CheckDate(form .arrivaldate.va lue) == false) {
    alert( "Please make sure your arrival date is at least 2 days in front of today's date" );
    form.arrivaldat e.focus();
    return false ;
    }
    [/CODE]

    [CODE=vb]<script language=vbscri pt>

    function CheckDate(sDate )

    if DateDiff("d", sDate, Now()) < 2 then
    CheckDate = false
    else
    CheckDate = true
    end if

    end function

    </script>[/CODE]
    Last edited by acoder; Feb 9 '08, 10:37 AM. Reason: Added code tags

    Comment

    • mrhoo
      Contributor
      • Jun 2006
      • 428

      #3
      var val='2008/02/7';// date input

      [CODE=javascript]// get todays date and subtract 2 days

      var d1=new Date();
      d1.setDate(d1.g etDate()-2);

      // or 48 hours: d1.setHours(d1. getHours()-48);
      // make a date object from the input-
      // this assumes January is input as 1, not 0:

      var d2= val.split(/\W+/);
      d2=new Date(d2[0]*1,d2[1]-1,d2[2]*1);

      //see if the input is after the limit:

      if(d2 > d1) alert('bad date')// throw error message
      else alert('good date');[/CODE]

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by scripto
        I am assuming "in front of" means "before" today's date - yes?<snip>
        Why use vbscript, when JavaScript can do it?

        PS. please use code tags.

        Comment

        • adamjblakey
          New Member
          • Jan 2008
          • 133

          #5
          Hi,

          I am trying:

          Code:
           var val=(form.arrivaldate.value);// date input 
          	 
          	var d1=new Date();
          	d1.setHours(d1.getHours()+48);
          	
          	// or 48 hours: d1.setHours(d1.getHours()-48);
          	// make a date object from the input-
          	// this assumes January is input as 1, not 0:
          	var d2= val.split(/\W+/);
          	
          	d2=new Date(d2[0]*1,d2[1]-1,d2[2]*1);
          	
          	//see if the input is after the limit:
          	if(d2 > d1) {
          	alert( "Please a date at least 2 days after today's date" );
              form.arrivaldate.focus();
              return false ;
          	}
          But this does not cover the 2 days after today's date. What this does is goes 2 days ahead and then does not allow any days after this. I need it to work for just the 2 days after today's date.

          Any Ideas?

          Cheers,
          Adam

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            d1 is two days after today's date. You need to change the if statement comparison around:
            [code=javascript]//see if the input is after the limit:
            if(d2 <= d1) {[/code]

            Comment

            • adamjblakey
              New Member
              • Jan 2008
              • 133

              #7
              Thanks for that, it works fine.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                You're welcome. Glad it helped.

                Comment

                Working...