Académique Documents
Professionnel Documents
Culture Documents
A Validation server control is used to validate the data of an input control. If the data does not pass validation, It will display an error message to the user.
The syntax for creating a Validation server control is: <asp:control_name id="some_id" runat="server" />
Validation Server Control Description Compares the value of one input control to the value of another input control or to a fixed value Allows you to write a method to handle the validation of the value entered Checks that the user enters a value that falls between two values Ensures that the value of an input control matches a specified pattern Makes an input control a required field Displays a report of all validation errors occurred in a Web page
CompareValidator
CustomValidator
Definition and Usage The CompareValidator control is used to compare the value of one input control to the value of another input control or to a fixed value. Note: If the input control is empty, the validation will succeed. Use the RequiredFieldValidator control to make the field required.
Property BackColor ControlToCompare ControlToValidate Description The background color of the CompareValidator control The name of the control to compare with The id of the control to validate The display behavior for the validation control. Legal values are: None (the control is not displayed. Used to show the error message only in the ValidationSummary control) Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation. Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation A Boolean value that specifies whether client-side validation is enabled or not A Boolean value that specifies whether the validation control is enabled or not
Display
EnableClientScript Enabled
The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set The foreground color of the control A unique id for the control A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid The type of comparison to perform. The operators are: Equal GreaterThan GreaterThanEqual LessThan LessThanEqual NotEqual DataTypeCheck Specifies that the control is a server control. Must be set to "server" The message to display when validation fails Specifies the data type of the values to compare. The types are: Currency Date Double Integer String A specified value to compare with
Operator
runat Text
Type
ValueToCompare
Examples CompareValidator Declare two TextBox controls, one Button control, and one CompareValidator control in an .aspx file. If validation fails, the text "Validation Failed!" will be displayed in red on a yellow background in the CompareValidator control. CompareValidator 2 Declare two TextBox controls, one ListBox control, one Button control, and one CompareValidator control in an .aspx file. The check_operator() function sets the operator of the CompareValidator control equal to the selected operator in the ListBox control, then it validates the CompareValidator control. If validation fails, the text "Validation Failed!" will be displayed in red on a yellow background in the CompareValidator control.
<asp:CompareValidator id="comvR" runat="server" display="static" controlToValidate="txtR" errorMessage="Rate must be numeric" type="Double" operator="DataTypeCheck"></asp:CompareValidator>
Properties
ASP.NET RangeValidator Control Definition and Usage The RangeValidator control is used to check that the user enters an input value that falls between two values. It is possible to check ranges within numbers, dates, and characters. Note: The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required. Note: The validation will not fail if the input value cannot be converted to the data type specified. Use the CompareValidator control, with its Operator property set to ValidationCompareOperator.DataTypeCheck, to verify the data type of the input value.
Property BackColor ControlToValidate Description The background color of the RangeValidator control The id of the control to validate The display behavior for the validation control. Legal values are: None (the control is not displayed. Used to show the error message only in the ValidationSummary control) Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation. Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation A Boolean value that specifies whether client-side validation is enabled or not A Boolean value that specifies whether the validation control is enabled or not The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set
Display
ForeColor id IsValid
The foreground color of the control A unique id for the control A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid Specifies the maximum value of the input control Specifies the minimum value of the input control Specifies that the control is a server control. Must be set to "server" Specifies the data type of the value to check. The types are: Currency Date Double Integer String The message to display when validation fails
Type
Text
Examples RangeValidator Declare one TextBox control, one Button control, and one RangeValidator control in an .aspx file. If validation fails, the text "The date must be between 1/1/2002 and 31/5/2002!" will be displayed in the RangeValidator control. RangeValidator 2 Declare one TextBox control, one Button control, one Label control, and one RangeValidator control in an .aspx file. The submit() function checks if the page is valid. If it is valid, it returns "The page is valid!" in the Label control. If it is not valid, it returns "The page is not valid!" in the Label control. If validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control.
<asp:RangeValidator id="ranvDependents" runat="server" display="static" controlToValidate="txtDependents" errorMessage="Must be from 0 to 10" type="Integer" minimumValue=0 maximumValue=10> </asp:RangeValidator>
Properties
Definition and Usage The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. Note: Both server- and client-side validation are performed unless the browser does not support client-side validation or the EnableClientScript property is set to false. Note: The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required.
Property BackColor ControlToValidate Description The background color of the RegularExpressionValidator control The id of the control to validate The display behavior for the validation control. Legal values are: None (the control is not displayed. Used to show the error message only in the ValidationSummary control) Static (the control displays an error message if validation fails. Space is reserved on the page for the message even if the input passes validation. Dynamic (the control displays an error message if validation fails. Space is not reserved on the page for the message if the input passes validation A Boolean value that specifies whether client-side validation is enabled or not A Boolean value that specifies whether the validation control is enabled or not The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set
Display
EnableClientScript Enabled
ErrorMessage
The foreground color of the control A unique id for the control A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid Specifies that the control is a server control. Must be set to "server" The background color of the RegularExpressionValidator control The message to display when validation fails Specifies the expression used to validate input control. The expression validation syntax is different on the client than on the server. JScript is used on the client. On the server, the language you have specified is used
ValidationExpression
Example RegularExpressionValidator Declare one TextBox control, one Button control, one Label control, and one RegularExpressionValidator control in an .aspx file. The submit() function checks if the page is valid. If it is valid, it returns "The page is valid!" in the Label control. If it is not valid, it returns "The page is not valid!" in the Label control. If validation fails, the text "The zip code must be 5 numeric digits!" will be displayed in the RegularExpressionValidator control.
<asp:RegularExpressionValidator id="regvH" runat="server" display="static" controlToValidate="txtH" errorMessage="Hours must be 1-3 digits only" validationExpression="\d{1,3}"> </asp:RegularExpressionValidator> CompareValidator:
Display
ForeColor id InitialValue
The foreground color of the control A unique id for the control Specifies the starting value of the input control. Default value is "" A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid Specifies that the control is a server control. Must be set to "server" The message to display when validation fails
IsValid
Declare two TextBox controls, one Button control, and one RequiredFieldValidator control in an .aspx file. It shows how to use the RequiredFieldValidator control to make the "txt_name" textbox a required field.
ClientValidationFunction
ControlToValidate
Display
EnableClientScript Enabled
ErrorMessage
The text to display in the ValidationSummary control when validation fails. Note: This text will also be displayed in the validation control if the Text property is not set The foreground color of the control A unique id for the control A Boolean value that indicates whether the control specified by ControlToValidate is determined to be valid Specifies the name of the server-side validation script function to be executed Specifies that the control is a server control. Must be set to "server" The message to display when validation fails
ForeColor id IsValid
Declare two Label controls, one TextBox control, one Button control, and one CustomValidator control in an .aspx file. The user() function checks the length of the input value. If the length is <8 or >16 the text "A username must be between 8 and 16 characters!" will appear in the CustomValidator control.
<asp:CustomValidator id="cusvDeptNum" runat="server" display="static" controlToValidate="txtDeptNum" onServerValidate="validateLength" errorMessage="Must be in multiples of 10" > </ asp:CustomValidator>
function validateLength(oSrc, args) { args.IsValid = (args.Value.length >= 10); }
The error message displayed in this control is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed for that validation control.
Property DisplayMode EnableClientScript Enabled ForeColor HeaderText id runat ShowMessageBox ShowSummary Description How to display the summary. Legal values are: BulletList List SingleParagraph A Boolean value that specifies whether client-side validation is enabled or not A Boolean value that specifies whether the validation control is enabled or not The fore color of the control A header in the ValidationSummary control A unique id for the control Specifies that the control is a server control. Must be set to "server" A Boolean value that specifies whether the summary should be displayed in a message box or not A Boolean value that specifies whether the ValidationSummary control should be displayed or hidden
Examples Validationsummary Use the ValidationSummary control to write a bulleted list of fields that are required but left empty by the user. Validationsummary 2 Use the ValidationSummary control to display a message box of the fields that are required but left empty by the user.
<asp:ValidationSummary id="valSummary" runat="server" headerText="Please correct the following errors" display="static" showSummary= "True" />
Server Controls
Validation Controls
Server Controls
Validation Controls
Use the IsValid property of class Page to check whether all the validators succeeded. You should always call method Validate before using property IsValid. When data is posted to the web server, the form s data becomes accessible to the web application through the properties of the various web controls.
20