ASP.Net Validation Controls Part 2
What if you want to check if a value falls in a certain range? That's what ASP.Net Range Validator is useful for, so let's see how it works.
Open the project we used in the ASP.Net Validation Controls Part1 Tutorial, type "Email:" next to the first TextBox, add another TextBox and name it txtAge, add another Button and name it btnAge.
Drop a Range Validator control on your page, set the ControlToValidate property to txtAge and change the ErrorMessage property to "Age must be between 6 and 100".
The key properties of the Range Validator control are the MinimumValue and MaximumValue so set the MaximumValue to 100 and the MinimumValue to 6, notice that those values are inclusive. Also, set the Type property to Integer, that tells ASP.Net to treat the values in the MinimumValue and MaximumValue as integers. Notice that you can select from Integer, String, Currency...etc depending on that values types of the MinimumValue and MaximumValue properties.
Test the page, enter your age and click the btnAge button. Oops what happened?
The Required Field Validator error message is shown and the form refuses to submit, what is happening, we clicked to btnAge button not the other one! It's because when you click a Button, all the page is validated! But what if we want to validate only the part of the page that we will use when we click a certain button? This is very common when you have multiple ASP.Net Buttons on the same page and is easy to fix using the ValidationGroup property which is any string you like, just make sure to use the same string for all the input controls, Validation Controls and the Button you want to be grouped together. So, set the ValidationGroup property of the txtAge, Required Field Validator and btnAge to "Age", run the page again and try different age values, every thing should now work as expected.
Another interesting validation control is the Compare Validator Control, which you can use to check if the value the user entered matches another control value -useful in cases like password changing- or if a value matches another static value -useful in cases like Captcha tests- so, let's learn how the ASP.Net the Compare Validator Control works.
Drop another two TextBoxes on the form, name them txtPassword and txtConfirm, drop another Button, name it btnChange and a Compare Validator Control, set the ErrorMessage of the Compare Validator Control to "Passwords do not match!".
In ASP.Net, password TextBoxes are ordinary TextBoxes with TextMode property set to Password so change it to Password for the txtPassword and txtConfirm TextBoxes, select both TextBoxes, the Compare Validator Control and the btnChange by pressing the Ctrl key and clicking them and set their ValidationGroup property to Password. Run or right click the page and choose "View in Browser" and it should work just fine.
If you want your Compare Validator Control to check for a certain value rather than the value entered in another control, you need to set the ValueToCompare property instead of the ControlToCompare, if you set both values, the ControlToCompare property will take precedence and this is the only one that will work. Aslo, you can change the Type property if you use values other than strings like you did in the Range Validator Control.
In the next part of the ASP.Net Validation Controls tutorial, we will lean more about those useful controls so keep checking Ask-Developer in the next few days!- How to Adjust Audio/Video Sync In An AVI File Using VirtualDub
Rating: 3.7/5 Votes: 173 - Dynamic Mask in Actionscript 3
Rating: 3.5/5 Votes: 11 - Fit Text in a Shape
Rating: 3.5/5 Votes: 113 - Show the Url of the Page
Rating: 3.5/5 Votes: 34 - Add Your Logo To A Video File
Rating: 3.5/5 Votes: 69







