Faizan Fayaz
Faizan Fayaz

Reputation: 568

How to validate date in yyyy-mm-dd format using javascript

How to validate date in yyyy-mm-dd format using javascript. so far i have come up with this code.

<script>  
  function checkdate(form)  
  {  
   var a = document.forms["form"]["stdt"].value; 


    Check = /^([0-9]{2})-([0-9]{2})-([0-9]{4})$/; 
    if(!a.match(Check)) {  
     alert("Error: Date format: ##-##-#### ");  
     return false;  
    }  

   }   
</script>

and HTML code

<form id="myform" onsubmit="return checkdate(this);"  method="post" action="">

The above will return true for 11-55-1999

Upvotes: 0

Views: 1143

Answers (2)

Irrech
Irrech

Reputation: 1394

I use moment, read the docs here http://momentjs.com/

It is simple to use, you have a value

var dateExample = '2016-08-23T16:09:11.690Z';
var DATE_FORMAT = 'YYYY/MM/DD';    
moment(dateExample).format(DATE_FORMAT)

Upvotes: 1

Rohan H
Rohan H

Reputation: 29

You can use Jquery vaidation for that as below

$('#frm').validate({
      rules: {
           date_creation: {
               required: true,
               date: true
           }
      }
   });

Upvotes: 0

Related Questions