Validate Date Entry In ASP.NET

  • Use the comparevalidator:
  • Set the ControlToValidate property to the conrol to validate
  • Set the Operator property to DataTypeCheck
  • Set the Type property to Date
 <asp:TextBox ID="tbPublishDateStart" runat="server"></asp:TextBox>

<asp:CompareValidator ID="cvPublishDateStart" runat="server" ErrorMessage="Please enter a valid start date" ControlToValidate="tbPublishDateStart" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator>
Advertisements

Change TextBox Border Color Identifying Issue During Validation

ASPX

<asp:textbox ID="textbox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="*" onservervalidate="CustomValidator1_ServerValidate" />
<asp:Button ID="Button1" runat="server" Text="Button" /> 

Code Behind

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (textbox1.Text == String.Empty)
{
textbox1.Style.Add("border", "solid 1px red");
args.IsValid = false;
}
}