07 April 2017

How Do You Validate Input Date Programmatically?

When validating a date, I recommend that you use both the client-side and the server side techniques. For client-side, you can use native JS or any JS framework available. For the server-side, you can do it programmatically such as the below sample:

<ASPX page>

Enter a date: <input type="date" id="txtExpirationDate" runat="server" required="" />
<p></p>
<label id="lblMessage" runat="server"></label>


<CODE BEHIND>

DateTime validDate;
if (DateTime.TryParse(txtExpirationDate.Value, out validDate))
{
    //some code goes here
}
else
{
    lblMessage.InnerText = "You entered an invalid date.";
}

No comments:

Post a Comment