Search code examples
jqueryhtmlvalidationdateasp-classic

Setting Input type date Minimum to Today and give validation error


I currently have this date picker on my insert form, however i'm unsure how to set the minimum value as today's date and how to receive a error message if the date when submitted is before today's date.

For this scenario i'm only allowed to use ASP classic, HTML5 and JQuery

N.B i have zero to little exposure to JQuery so i did not understand many of the solutions already available.

<input type="date" name="expirydate" id="expirydate" size="60%"  required>  

Solution

  • Here is a way to get the error message which gives when the date selected is less than today's date and gets success message when the date selected is greater than today's date.

    We can use date function getTime() which returns time for the specified date according to universal time to solve this.

    Read more about Date functions here MDN

    var date = document.getElementById('expirydate');
    date.addEventListener('change', function(){
      var d = new Date();
      //console.log(d);
      var nd = new Date(this.value);
      
      if(new Date(this.value).getTime() <= d.getTime()){
        console.log("Error, Date must be greater than today");
      }
      else{
        console.log("success")
      }
    })
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
    <input type="date" name="expirydate" id="expirydate" size="60%"  required>  
    </body>
    </html>