Section 8.6. Custom Validation

8.6. 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 point to 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 a server-side and a client-side method for validation. The server-side method can be written in any .NET language, such as C# or VB.NET, but the client-side method must be written in a scripting language understood by the browser, such as VBScript or JavaScript.

Once again, create a new web site, named CustomValidator , and copy the RegularExpressionValidator web site to get you started. In this example, you want to ensure that the user enters an even number.

This time, you'll report an error if the number is not evenly divisible by 2. You can imagine, however, that you could use this technique perform a checksum on a credit card or ISBN or otherwise perform complex data checking.

Most of these checks can be done more easily with a Regular Expression Validator; the custom validator should be used only as a last resort.


Replace the RegularExpressionValidator with a CustomValidator . Set the ControlTo-Validate field, and set EnableClientScript to TRue (the default). Set the text property to Please enter an even number .

CustomValidators have an additional property that can save you a lot of special coding: ValidateEmptyText .

 ValidateEmptyText=false 

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

The key to making your custom validator work is in setting the client-side validator, which you do in the ClientValidationFunction property (set this to ClientValidator ). Also, click the Events lightning bolt button and set the ServerValidate event handler to ServerValidator . Each of these is the name of a method. The first is a JavaScript method that you'll add to the content file ( default.aspx in this case):

 <script language="javascript">    function ClientValidator(source, args)    {       if (args.Value % 2 == 0)          args.IsValid=true;       else          args.IsValid=false;       return;    } </script> 

This function examines the value passed to the script by the validator, and if it is an even number, it will return TRue ; otherwise, it will return false .

You'll implement the server-side method in the code behind file, default.aspx.cs :

 protected void ServerValidator (object source, ServerValidateEventArgs e) {    try    {       e.IsValid = false;       int evenNumber = Int32.Parse(e.Value);       if (evenNumber % 2 == 0)          e.IsValid = true;    }    catch (Exception)    {       // error handler here    } } 

This method does the same thing as the client-side validator, only in C# rather than in JavaScript. There are a few things to notice about these methods . First, the value that the CustomValidator is examining is passed to your routine as the Value property of the ServerValidateEventArgs event argument. You convert that string to an int using the Base Class Library Int32 object's static Parse method, as shown.

The declaration for the CustomValidator in the content file sets the client-side method and the server-side method you've designated.

 <asp:CustomValidator runat="server"    ID="CustomValidator1"    ControlToValidate="txtValue"    ValidateEmptyText=false  ClientValidationFunction="ClientValidator"    OnServerValidate="ServerValidator">  Please enter an even number </asp:CustomValidator> 

If you run this program in an uplevel browser and enter an odd number, the page will never be posted back to the server; the JavaScript handles the validation. If you enter an even number, however, the client-side script and the server-side script will run (to protect against spoofing from the client).



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

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