Powered by Blogger.

Thursday, February 27, 2014

Date validation using JavaScript




In this post we will discuss about date validation using JavaScript. Also check out:

- Create a database by command in sql server 2008

- RegularExpressionValidator example in Asp.Net

- Export Data Of DataGridview To Excel using C# Windows Appllication

Here is a date validation through JavaScript that will give an alert message if you try to give a date less than today. It will always take the future date.

Below is the full HTML code:

 <head runat="server">
 <script language="javascript" type="text/javascript">
     function validateDate() {
         var expDate = document.getElementById('txtDate').value;
         var date = expDate.substring(0, 2);
         var month = expDate.substring(3, 5);
         var year = expDate.substring(6, 10);
         var myDate = new Date(year, month - 1, date);
         var today = new Date();

         if (myDate > today) {
         }
         else {
             alert('Please enter a date greater than today !');
             return false;
         }
     }
    </script>
    <title>JavaScript date validation</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnValidate" runat="server" Text="Click to Validate" OnClientClick="return validateDate();" />
    </div>
    </form>
</body>

Check the figure below for more information:



0 comments

Post a Comment