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

asked on

How would you go about validating a field and throw an error if the field is empty because this is a REQUIRED field (not null)?

I am developing an ASP.NET application using VS2015 and .NetFramework 4.6.1

I use the follow text box control:

How would you go about validating this field and throw an error if this field is empty because this is a REQUIRED field (not null?

 <asp:Label ID="lblCheck" Text = "Check:" runat ="server" ForeColor="#00356A" Width="10%" />
                        <asp:TextBox ID="txtChecknum" Maxlength="6" runat="server" style="margin-left: 0px;text-align: left;" Width="15%" ></asp:TextBox>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                        ErrorMessage="Check Number Invalid. Enter only numbers."
                        ControlToValidate="txtChecknum"
                        SetFocusOnError="true"
                        ValidationExpression="^\d+$">
                        </asp:RegularExpressionValidator>
Avatar of zimmer9
zimmer9
Flag of United States of America image

ASKER

I tried the following but no error pops up if I leave the text box blank.

<asp:Label ID="lblCheck" Text = "Check:" runat ="server" ForeColor="#00356A" Width="10%" />
                        <asp:TextBox ID="txtChecknum" Maxlength="6" runat="server" style="margin-left: 0px;text-align: left;" Width="15%" ></asp:TextBox>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                        ErrorMessage="Check Number Invalid. Enter only numbers." Display="Dynamic"
                        ControlToValidate="txtChecknum"
                        SetFocusOnError="true"
                        ValidationExpression="^\d+$">
                        </asp:RegularExpressionValidator>
                        <asp:RequiredFieldValidator ID="rfvcandidate"
                        runat="server" ControlToValidate ="txtChecknum"
                        ErrorMessage="Required field. Numbers only."  Display="Dynamic"
                        InitialValue="Please input only numbers">
                        </asp:RequiredFieldValidator>
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India 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