How to validate a textbox for range of values,correct date?

How to validate a textbox for range of values,correct date?

In scenarios where we want to validate a textbox for the specific range of values, and ensure that the correct Date to be entered, we can follow this approach.

Validations
Validation controls are used to validate the form fields for valid data when it is submitted to the server.
  • In case we want to validate a textbox for the specific range of values to be entered, we can follow this approach.
ASPX PAGE 

Enter year between 2000 to 2010:
<asp:TextBox ID="txtRange" runat="server" /><br />
<asp:RangeValidator ID="range1" runat="server" ErrorMessage="Sorry, must be
between 2000 and 2010" ControlToValidate="txtRange" MaximumValue="2010" MinimumValue="2000"
Type="Integer" ForeColor="Red" />
<p>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</p> 

In the above code snippet, we have a TextBox that must accept the value in between 2000 and 2010. To validate this range, we can use asp:RangeValidator with MaximumValue as 2010 and MinimumValue as 2000 with Type as “Integer”. This causes the TextBox to accept only values in between 2000 to 2010.
Note: The asp:RangeValitor only works if any data is entered to the TextBox, if no data is entered and button is clicked the form is submitted to the server without validation. To avoid this, we should keep asp:RequiredFieldValidator attached with the TextBox to validate.


Note: Similarly Date, Double, String, Currency values can be validated for specific range.

  • In case we want to validate a textbox value for the correct date, we can follow this approach.

ASPX PAGE

 

Date of birth:
<asp:TextBox ID="txtDOB" runat="server" /><br />
<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !" ControlToValidate="txtDOB"
ForeColor="Red" />
<asp:CompareValidator ID="comp1" runat="server" ErrorMessage="Date must be in
MM/DD/YYYY format" ControlToValidate="txtDOB" Type="Date" Operator="DataTypeCheck" ForeColor="Red" />
<p>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</p> 


In the above code snippet, we have a TextBox that must accept correct date. To do that we can use asp:CompareValidator with Operator as “DataTypeCheck” and Type as “Date”. We have used asp:RequiredFieldValidator to force user to enter some data into the TextBox before the form is submitted to the server.


  • In scenarios where we want to check whether a valid email id has been entered into the TextBox or not, we can follow this approach.

ASPX PAGE

 

Your email:
<asp:TextBox ID="txtEmail" runat="server" Columns="50" />
<br />
<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !" ControlToValidate="txtEmail"
ForeColor="Red" />
<asp:RegularExpressionValidator ID="ReExp1" runat="server" ControlToValidate="txtEmail"
ErrorMessage="eg. abc@domain.com" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-
.]\w+)*"></asp:RegularExpressionValidator>
<p>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" />
</p> 


CODE BEHIND
 
protected void SubmitData(object sender, EventArgs e)
{

}


In the above code snippet, we want to validate the TextBox for correct format of the email id. For that we have used asp:RegularExpressionValidator with ValidationExpression as “\w+([-+.']\w+)*@\w+([- .]\w+)*\.\w+([-.]\w+)*” that validates the TextBox data for correct email id format (eg. abc@domain.com).
We have used asp:RequiredFieldValidtor to force user to enter some data into the TextBox.

Hope this Post was useful. Thanks for reading.

Keep reading my forth coming Post s. To read my series of articles on ASP.NET,