Skip navigation
JoshuaBr
Currently Being Moderated

How to set a minvalue = the current date

Jul 29, 2012 9:50 PM

Tags: #cs5.5 #dreamweaver #spry #dreamweaver_cs5 #widget

Hi,

 

I have a website where a user must enter a date that is not in the past, ie today or greater, and I want to use validation to ensure that the user entered data is not in the past. How do I set the minvalue to today's date dynamically so that I don't have to change the minvalue every day at midnight?

 

Thanks in advance,

Joshua

 

FYI, I'm using DW CS 5.5 and Spry 1.6.1, and the Spry validation textfeild with the 'date' type selected to gather the user input.

 
Replies
  • Currently Being Moderated
    Jul 30, 2012 12:43 AM   in reply to JoshuaBr

    Try

    <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Untitled Document</title>
      <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet">
    </head>
    
    <body>
      <p id="spryDate">
        <label for="chosenDate">Date:</label>
        <input type="text" name="chosenDate" id="chosenDate">
        <span class="textfieldRequiredMsg">A Date is Required.</span> 
        <span class="textfieldInvalidFormatMsg">Incorrect Date.</span>
      </p>
    
      <script src="SpryAssets/SpryValidationTextField.js"></script>
      <script>
            var spryDate = new Spry.Widget.ValidationTextField("spryDate", "custom", {validation:checkdate, validateOn:["blur"]});
            function checkdate(value) {
                var entry = value.split('/');//splits input value;
                var y = entry[2]*1;
                var m = entry[1]*1-1;//sets months to default values i.e Jan = 0; Feb = 1; etc.
                var d = entry[0]*1;
                var newDate = new Date(y,m,d); //sets the chosen date
                var today = new Date();
                dif = newDate-today;//difference in milliseconds
                dif = Math.ceil(dif/1000/60/60/24);//difference in days
                if(dif >= 0){//if the date is not in the past, test if date is valid
                    return /^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/.test(value);
                } else {
                    return false;
                }
            }
      </script>
    </body>
    </html>

     

    Gramps

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 30, 2012 1:12 AM   in reply to JoshuaBr

    You may have to adapt to your locale (i.e., date format), but Spry does check against a minimum date:

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Untitled Document</title>

    <script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>

    <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />

    </head>

     

     

    <body>

    <form id="form1" name="form1" method="post" action="">

      <span id="sprytextfield1">

      <label for="txtDate">Date:</label>

      <input type="text" name="txtDate" id="txtDate" />

      <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMinValueMsg">The entered value is less than the minimum required.</span></span>

      <input type="submit" name="button" id="button" value="Submit" />

    </form>

    <script type="text/javascript">

    var today = new Date();

    var minDate = ('0' + (today.getMonth() + 1)).substr(-2,2) + '/' + ('0' + today.getDate()).substr(-2, 2) + '/' + today.getFullYear();

    var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {validateOn:["blur"], minValue:minDate, hint:"mm/dd/yyyy", format:"mm/dd/yyyy"});

    </script>

    </body>

    </html>

     

    Xavier

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 2:13 PM   in reply to JoshuaBr

    As soon as I Xavier's solution, I knew that it was the better one, making much better use of the facilities already provided by Spry.

     

    Thanks for the feedback.

     

    @Xavier - Good show!!

     

    Gramps

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 31, 2012 10:58 PM   in reply to Ben Pleysier

    Thanks, Gramps.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points