MIS 3200 – Unit 2.2 Outline What’s the problem with bad data? Traditional solutions ASP.NET tools – Validators RequiredFieldValidator RangeValidator CompareValidator ValidationSummary
What’s the problem BAD DATA! Bad input data Incorrect format Illegal values Illogical values Illegal characters Illegal operations (e.g. division by zero) 03/30/2013 03/30/12 -5/03/2012 30/03/2012 ##/03/2012
Traditional Solutions Data entry professionals Double entry procedures Self-checking numbers Program code
Solution: Validators Controls that do error checking, such as Require an entry: Provide a credit card number Within a specified range Year of birth should be between 1900 and 2000 Compare an entry to a predetermined value Retype your password, and they should match Compare an entry the value of another control Require an email address to contain “@” and “.”
How Validators Work ControlToValidate property tells the validator which TextBox to validate Placed next to the control they to validate User feedback Text ErrorMessage “Fire” (be triggered) when a button is clicked or when focus passes to another control. Remember these user feedback properties
RequiredFieldValidator - RFV Typically associated with a TextBox Fires if nothing is entered in the TextBox When the validator fires it displays a message If only one of the validator’s ErrorMessage and Text properties has a value then that value is displayed If both ErrorMessage and Text have values then the value of Text is displayed at the location of the validator and the value of ErrorMessage is displayed in a ValidationSummary control if there is one on the page
RFV Example txtNum1 Running version before pressing Try It Running version after pressing Try It Error message color set to red No value set for Text
RFV Example 2 Validation problems often need more text than what can be accommodated next to the web control. Use ValidationSummary control and the ErrorMessage property linked to all validators on the page Design view After Running and pressing Try It
CompareValidator - CV Compare an entry to a predetermined value ControlToCompare if you want to compare to another Control or the value of another control ValueToCompare to compare to a predetermined value To prevent missing data while comparing, MUST be used with a RequiredFieldValidator CV only fires if there is something to validate
CV Example Allows you to chose the type of comparison Allows you to chose the data type Default type is string Allows you to chose the type of comparison Default is a test for equality Allows you to set focus on the problematic data when the validator fires Recall: the Focus() method
CV Example To compare to another control RequiredFieldValidator CompareValidator Entry is not > 10 Entry is a whole number > 10 Entry is not a whole number
RangeValidator - RV Has the same Type choices as the CV Similar to the CompareValidator but has a MinimumValue and a MaximumValue (instead of a ValueToCompare or FieldToCompare) Has the same Type choices as the CV Has the same SetFocusOnError option
RV Example An entry is required Please enter a number in the range RFV and RV Note: this works fine for data types double and decimal
Try it Out – L2.2 Make sure ASPPub is on your desktop Start VWD and open ASPPub Open the Unit2 folder under MIS3200 Click on yourLastNameU2L2.aspx file Right-click and copy Click on the Unit2 folder Right-click and paste Change the file name to yourLastNameU2L22.aspx
L2.2 #2 Open the file in Design view Drop a RequiredFieldValidator to the right of your first TextBox Change the (ID) to rfvNum1 Change the ControlToValidate to txtNumber1 Change the ErrorMessage to Number 1 is required Change the Text to * Change ForeColor to red Change SetFocusOnError to true Change the Display to Dynamic
L2.2 #3 Drop a CompareValidator to the right of your RequiredFieldValidator Set the (ID) to cvNumber1 Set ControlToValidate to txtNumber1 Change ErrorMessage to Number 1 must be a valid number Change Text to * Change SetFocusOnError to true Change the ForeColor to red Change Operator to DataTypeCheck Change Type to Double Change the Display to Dynamic
L2.2 #4 Repeat steps 10 and 11 for the second TextBox, changing names and messages as required Drop another CompareValidator after the second TextBox Repeat all of step 11 again for the 2nd CV with the following differences: Set the (ID) to cvNumber2Zero Set the error message to Number 2 can’t be zero Set Operator to NotEqual Set ValueToCompare to 0 (a zero) Set the ControlToValidate to txtNumber2 Do NOT set the Type (if you do delete the validator and repeat)
L2.2 #5 Drop a ValidationSummary after lblOutput Change its ForeColor to red Run the page trying various combinations of values Press + with nothing in the text boxes Put a number in one text box but not the other Put non-numeric data in one text box or the other Put zero in the second text box Change the page heading to Unit 2.2 L2.2 – Simple Calculator with Validation
L2.2 – Sample Output
L2.2 – Final steps Open your MIS3200 home page in the MIS3200 folder Add another hyperlink for unit 2 (ID) = hlU22L22 Text = Unit 2.2 L2.2 NavigateUrl = the page you just modified Copy ASPPub back to ASPNET and submit your MIS Portfolio URL to the drop box
Think About It! Why are validators important? When do you use the following: RequiredFieldValidator RangeValidator CompareValidator ValidationSummary