Hi Tevin,
Refer below code.
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/json2/20160511/json2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#<%=txtFromDate.ClientID%>').datepicker({
dateFormat: 'dd/mm/yy'
});
$('#<%=txtToDate.ClientID%>').datepicker({
dateFormat: 'dd/mm/yy'
});
});
</script>
From:
<asp:TextBox ID="txtFromDate" runat="server" />
<asp:Label ID="lblFrom" Text="Enter only dd/MM/yyyy format." runat="server" Visible="false" ForeColor="Red" />
<br />
To:
<asp:TextBox ID="txtToDate" runat="server" />
<asp:Label ID="lblTo" Text="Enter only dd/MM/yyyy format." runat="server" Visible="false" ForeColor="Red" />
<br />
<asp:Button Text="Submit" runat="server" OnClick="OnSubmit" />
Code
C#
protected void OnSubmit(object sender, EventArgs e)
{
lblFrom.Visible = false;
lblTo.Visible = false;
DateTime dtpFromDate;
DateTime dtpToDate;
if (!DateTime.TryParseExact(txtFromDate.Text.Trim(), "dd/MM/yyyy",
System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dtpFromDate))
{
lblFrom.Visible = true;
ClientScript.RegisterStartupScript(this.GetType(), "alertFrom", "alert('Invalid From Date.');", true);
}
else
{
dtpFromDate = DateTime.ParseExact(txtFromDate.Text.Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture);
}
if (!DateTime.TryParseExact(txtToDate.Text.Trim(), "dd/MM/yyyy",
System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dtpToDate))
{
lblTo.Visible = true;
ClientScript.RegisterStartupScript(this.GetType(), "alertTo", "alert('Invalid To Date.');", true);
}
else
{
dtpToDate = DateTime.ParseExact(txtToDate.Text.Trim(), "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture);
}
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
lblFrom.Visible = False
lblTo.Visible = False
Dim dtpFromDate As DateTime
Dim dtpToDate As DateTime
If Not DateTime.TryParseExact(txtFromDate.Text.Trim(), "dd/MM/yyyy",
Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None, dtpFromDate) Then
lblFrom.Visible = True
ClientScript.RegisterStartupScript(Me.GetType(), "alertFrom", "alert('Invalid From Date.');", True)
Else
dtpFromDate = DateTime.ParseExact(txtFromDate.Text.Trim(), "dd/MM/yyyy", Globalization.CultureInfo.CurrentCulture)
End If
If Not DateTime.TryParseExact(txtToDate.Text.Trim(), "dd/MM/yyyy",
Globalization.CultureInfo.CurrentCulture, Globalization.DateTimeStyles.None, dtpToDate) Then
lblTo.Visible = True
ClientScript.RegisterStartupScript(Me.GetType(), "alertTo", "alert('Invalid To Date.');", True)
Else
dtpToDate = DateTime.ParseExact(txtToDate.Text.Trim(), "dd/MM/yyyy", Globalization.CultureInfo.CurrentCulture)
End If
End Sub
Screenshot