Valid date with format validation in javascript using regular expresion (With leap year consideration)


Valid date with format validation in javascript using regular expresion


Before this you may need required field validation using javascript. In this example, we are validating correct date with leap year into consideration using regular expression.

For more form validations go here.



<html>
<head>
    <title></title>
    <script type="text/javascript">
        function ValidateForm() {
            var txtDate = document.getElementById("txtDate").value;
            var regularExpression = /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/; 
            if (!txtDate.match(regularExpression)) {
                alert("Please enter date format.Ex: 05/05/2012");            
            }
        }
    </script>
</head>
<body>
Date: <input type="text" id="txtDate" /><br />
<br />
<input type='button' value="validate" onclick="ValidateForm();" />

</body>
</html>

Read more ASP.NET, C#.NET and SQL Server interview questions.

If you have any queries or suggestions, please feel free to ask in comments section.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar