TOC

This article is currently in the process of being translated into Russian (~29% done).

Validation:

Validation - RangeValidator

RangeValidator делает именно то, что подразумевает его английское название: он проверяет, что значения, введенные пользователем, находятся в определенном промежутке. Его можно использовать для проверки введенных чисел, строк или дат, что может быть полезно в ряде случаев. Поскольку в прошлый раз мы использовали валидатор для проверки числовых значений, в этот раз мы попробуем проверить значения даты.

Date:<br />
<asp:TextBox runat="server" id="txtDate" />
<asp:RangeValidator runat="server" id="rngDate" controltovalidate="txtDate" type="Date" minimumvalue="01-01-2006" maximumvalue="31-12-2006" errormessage="Please enter a valid date within 2006!" />
<br /><br />

The date format might seem a bit weird to you if you're not from Europe, where we use dd-mm-yy. You can just change it if it doesn't fit the date format on the machine you're working on. Now, try running the website, and enter a date in our new TextBox. It's only valid if the date is within 2006, and a cool sideeffect is that the date is also checked for validity. Have a look at this screenshot, which shows us that the validator reacts to an impossible date as well:

And once again, if the clientside validation is not working, it will be catched in our CodeBehind, as shown with the RequiredFieldValidator. If you missed this part, then be sure to check out the first chapters!


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!