Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

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="ftxtdtCreated"
                    ErrorMessage="Must be a valid date" Operator="DataTypeCheck"
                    SetFocusOnError="True" Type="Date">
                    </asp:CompareValidator>
                 <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"
                       ErrorMessage="Must be a valid date"
                       ControlToValidate="ftxtdtCreated"
                       SetFocusOnError="true"
                       ValidationExpression="^\d+\/\d+$">                    
                 </asp:RegularExpressionValidator>  
                 </FooterTemplate>
ASKER CERTIFIED SOLUTION
Avatar of Elvio Lujan
Elvio Lujan
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial