zimmer9
asked on
How to ensure the user enters a value in a date field in an ASP.NET application?
I am developing an ASP.NET application using VS2015 and .Net Framework 4.6.1
I want to ensure that an error message appears if the user leaves the field empty.
What am I doing wrong? The following ensures that a correct date is entered but does not work if the user leaves the field empty.
<asp:TextBox ID="ftxtdtCreated" Width="200px" MaxLength="10" MaximumValue="9999" MinimumValue="1" runat="Server" Text='<%# Eval("dtCreated") %>'></asp:TextBox>
<asp:CompareValidator ID="cvtxtdtCreated" runat="server" ControlToValidate="ftxtdtC reated"
ErrorMessage="Must be a valid date" Operator="DataTypeCheck"
SetFocusOnError="True" Type="Date">
</asp:CompareValidator>
<asp:RegularExpressionVali dator ID="RegularExpressionValid ator3" runat="server"
ErrorMessage="Must be a valid date"
ControlToValidate="ftxtdtC reated"
SetFocusOnError="true"
ValidationExpression="^\d+ \/\d+$">
</asp:RegularExpressionVal idator>
</FooterTemplate>
I want to ensure that an error message appears if the user leaves the field empty.
What am I doing wrong? The following ensures that a correct date is entered but does not work if the user leaves the field empty.
<asp:TextBox ID="ftxtdtCreated" Width="200px" MaxLength="10" MaximumValue="9999" MinimumValue="1" runat="Server" Text='<%# Eval("dtCreated") %>'></asp:TextBox>
<asp:CompareValidator ID="cvtxtdtCreated" runat="server" ControlToValidate="ftxtdtC
ErrorMessage="Must be a valid date" Operator="DataTypeCheck"
SetFocusOnError="True" Type="Date">
</asp:CompareValidator>
<asp:RegularExpressionVali
ErrorMessage="Must be a valid date"
ControlToValidate="ftxtdtC
SetFocusOnError="true"
ValidationExpression="^\d+
</asp:RegularExpressionVal
</FooterTemplate>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.