Browse DevX
Sign up for e-mail newsletters from DevX


Validating Data On Web Forms : Page 6

There are six validation controls included with ASP.NET as well as a summary control. Data validation with the ASP.NET validation controls is always processed on the Web server and can also optionally be processed on the user's computer.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

CustomValidator Control
To perform custom validation beyond that already provided by the other ASP.NET validation controls, the CustomValidator control processes a custom script that must return true or false to indicate success or failure of the validation process.

ASP.NET processes all validation controls on the server and, optionally, on the user's computer. You must write a custom function member for the CustomValidator to process on the Web server. You may also write a script for the CustomValidator to process on the user's computer.

In the HTML snippet below, a CustomValidator validates the value entered into a TextBox control to determine if the value is a prime number.

<asp:TextBox ID="txtPrimeNumber" RunAt="Server" /> <asp:CustomValidator ClientValidationFunction="ClientValidation" ControlToValidate="txtPrimeNumber" Display="Static" ErrorMessage="The number entered is not a prime number." ID="cvPrimeNumber" RunAt="Server" OnServerValidate="ServerValidate" />

Here I've specified the txtPrimeNumber TextBox control as the control to validate using the ControlToValidate attribute of the CustomValidator control. I want the CustomValidator control to process two scripts: ClientValidation and ServerValidate, which I specify using the ClientValidationFunction attribute and the OnServerValidate attribute, respectively.

The ClientValidationFunction attribute designates the validation script that the CustomValidator control will process if the EnableClientScript attribute has a value of true. The function name you specify in your validation script must match the value you specified in the ClientValidationFunction attribute. I wrote the validation script in this example using JavaScript.

<script type="text/JavaScript"> <!-- function ClientValidation(source, arguments) { for(var counter=2; counter<=arguments.Value/2; counter++) { if((arguments.Value%counter)==0) { arguments.IsValid = false; return false; } else { arguments.IsValid = true; return true; } } } --> </script>

When the CustomValidator calls the ClientValidationFunction, it passes a reference to the calling object as well as the attributes associated with the calling object. The Value property of the txtPrimeNumber TextBox control as well as the IsValid property are accessed through the arguments parameter that is passed.

The OnServerValidate attribute identifies the function member to process on the Web server for data validation. This function member can carry out a completely different process than the ClientValidationFunction. However, in most cases, the function member on the Web server and the script on the user's computer are identical in functionality. Here I've used C# to write the ServerValidate function member used in this example.

protected void ServerValidate(object sender, ServerValidateEventArgs e) { int primeNumber = int.Parse(e.Value); for (int counter = 2; counter <= (primeNumber/2); counter++) { if ((primeNumber % counter) == 0) { e.IsValid = false; return; } else { e.IsValid = true; return; } } }

The function member shown above is identical in functionality to the validation script I wrote in JavaScript. Figure 6 shows the CustomValidator control in action.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date