Data Validation


Because this sample code uses the Visual Basic Val function to preprocess the user-supplied data, it almost always works without error. Any data considered invalid is simply converted to zero. Another option would be to chastise the user for invalid entry before processing occurs, to validate the supplied values. The five validators in the Validation section of the Web Forms Toolbox help you do just that.

  • RequiredFieldValidator. Confirms that the user supplied any value at all in a control.

  • RangeValidator. Complains if a control's value does not fall between two values.

  • RegularExpressionValidator. Lets you compare a control's value against a regular expression pattern. For instance, you could compare the user's entry of a serial number to a pattern to ensure that it included two letters followed by five digits.

  • CompareValidator. Involves two controls, comparing the value between them. The control also doubles as a data type validator, confirming that a single field contains the proper type of data, such as a date value or an integer.

  • CustomValidator. Lets you perform any type of validation you want through code you supply.

All of these controls perform server-side validation, and they optionally do their data check using client-side scripts (the default). Having the client-side check reduces the need to go back to the web server just to ensure a required field has data. Having the server-side check ensures that the data is valid even if the client has disabled scripting support.

The validators display their own error messages, so you place them on the page where you want the error message to appear. You can also have multiple validators display their collective issues in a single location by using a ValidationSummary control.

Let's add some validation to the two input fields in the multiplication sample. We want to ensure that the data is supplied, and that both values are valid integers. To do this, we must add both a RequiredFieldValidator and a CompareValidator for each field. Right-click in the bottom-right cell of the table, just after the Product label, and choose Insert Column to the Right from the shortcut menu that pops up. In the new upper-right column, add a RequiredFieldValidator control. Set the following properties:

  • Set ControlToValidate to "FirstOperand."

  • Set Display to "Dynamic." This lets the size of the validator shrink to nothing when there is no error to display.

  • Set ErrorMessage to "Missing."

Just to the right of that validator, in the same table cell, add a CompareValidators, and set these properties:

  • Set ControlToValidate to "FirstOperand."

  • Set ErrorMessage to "Must be an integer."

  • Set Operator to "DataTypeCheck."

  • Set Type to "Integer."

Add a similar pair of validators to the second table row, using "SecondOperand" for the ControlToValidate. Your web page should look like Figure 22-8.

Figure 22-8. Bulking up on the validation support


Run the program and just try to enter faulty data in the input cells. The page will complain immediately when you click the Multiply button.

That's all the multiplying we'll do for now. I've saved a copy of the project for you in the WebSite1 subdirectory in the main directory where you installed this book's sample code.




Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

Similar book on Amazon

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