9.7. Custom Validation


There are times when the validation of your data is so specific to your application that you will need to write your own validation method. The CustomValidator is designed to provide all the infrastructure support you need. You provide the name of your validation method and have it return a Boolean value: true or False. The CustomValidator control takes care of all the rest of the work.

Because validation can be done on the client or on the server, depending on the browser, the CustomValidator has attributes for specifying both a server-side and a client-side method for validation. The server-side method can be written in any .NET language, (we, of course will use Visual Basic 2005), while the client-side method must be written in a scripting language understood by the browser, such as VBScript or JavaScript.

Add one more row to your form (below the ZIP-code row), asking the user for an even number, as shown in Figure 9-23.

Figure 9-23. Even number row


Once again, the middle column has a text box (txtEven). Drag a CustomValidator into the third column and set its attributes as shown in Table 9-9.

Table 9-9. CustomValidator properties and events

Property

Value

ID

cvEven

ControlToValidate

txtEven

ClientValidationFunction

ClientEventValidator

ValidateEmptyText

False

ErrorMesssage

Your number is rather odd

Text

*

ServerValidate [EVENT]

ServerEventValidate


The CustomValidator takes three new elements. The first, OnServerValidate, points to the server method that will be called to perform validation . You'll add this to the code-behind page, as shown in Example 9-3.

Example 9-3. Server method to perform validation
 Protected Sub ServerEvenValidate(ByVal source As Object, _             ByVal e As ServerValidateEventArgs)    Dim evenNumber As Int32 = Int32.Parse(e.Value)    If evenNumber Mod 2 = 0 Then       e.IsValid = True    Else       e.IsValid = False    End If End Sub 

The second new element is the ClientValidationFunction. This is a script function that will be called to validate the user's entry, client-side, before your form is submitted, as shown in Example 9-4. Place this script in the header section of your content (.aspx) file.

Example 9-4. Client-side validation script
 <script language="javascript">    function ClientEvenValidator(source, args)    {      if (args.Value % 2 =  = 0)        args.IsValid=true;      else        args.IsValid=false;      return;    } </script> 

If the value is not even, the server-side method sets e.IsValid to False. Similarly, the client-side method sets args.IsValid to False. In either case, the error is displayed.

Build and run the application. Enter an odd number into this field. You should see the error message shown in Figure 9-24.

Figure 9-24. CustomValidator


The third new property, and one that can save you a lot of special coding, is ValidateEmptyTest:

     ValidateEmptyText=false 

The default is true, but by setting it to False, the text field will be considered invalid if it is empty.

Unfortunately, this only works on the CustomValidator.


ValidateEmptyTest avoids the need for a RequiredFieldValidator (and, boy, do I wish they added this attribute to the other validators as well!).



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net