In the above code snippet, on .aspx page we have two sets of Login form (User Login and Admin Login) separately. To validate both forms separately we can distinguish their controls using ValidationGroup property. Notice that both forms asp.net server controls have “LoginForm1” and “LoginForm2” as ValidationGroup that causes the forms to be validated separately.

OUTPUT

How to validate a textbox for range of values?

In case we want to validate a texbox for the specific range of values, we can follow this approach.

ASPX PAGE

Enter year between 2000 to 2010: <asp:TextBoxID="txtRange"runat="server"/><br/>

<asp:RangeValidatorID="range1"runat="server"ErrorMessage="Sorry, must be

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.

Notice: The asp:RangeValitor only works if any data is entered to the TextBox, if no data is entered and button is clicked the form get submitted to the server. To avoid this, we should keep asp:RequiredFieldValidator attached with the TextBox to validate.

In the above code snippet, we have a TextBox that must accept correct date. To do that we can useasp:CompareValidator with Operator as “DataTypeCheck” and Type as “Date”. We have usedasp:RequiredFieldValidator to force user to enter some data into the TextBox before the form is submitted to the server. When user enter some data and that is not in the correct date format, he/she gets message as displayed in the picture below.

OUTPUT

In case you have missed the 1st article in this series, click here to read.

Hope you are liking the articles in this series. Do let me know if you have any question.

Do not forget to subscribe for the RSS feed to get the new articles alert directly in your Inbox, follow on tweeter at @dotnetfunda to get new post alert !