The validation controls [ASP.NET 2.0]
The validation controls allow to verify the validity of a Form field either at the validation of the form or when it is being entered.A validator is a particular server control that allows you to do validating input by the user.
Data validation is to check that a date is recovering well where the user must enter a date,that a number is recovering well where you must collect a number and not a string.
Summary
1-RequiredFieldValidator: check if a field is empty
2 - RangeValidator: Check that the entry of a field is ranged in an interval of 2 values
3 - CompareValidator: comparing the value of a field relative to the value of another field or to a defined value
4 - RegularExpressionValidator: compare the value of a field against a regular defined expression
5 CustomValidator: define a custom validation
6 - ValidationSummary: list all the errors with a control type "summary"
7 Me.validate or this.Validate (): validate a form programmatically
8 - ValidationGroup: create groups of validation for a form1 - RequiredFieldValidator:
Make MANDATORY the SEIZURE or SELECTION IN A FIELD
This is when you press the send button that all fields will be checked and the error messages displayed
Dragging from tab validation of the toolbox
- ControlToValidate: ID of the control to validate / verify
- InitialValue: It is relative to this value that a comparison will be made (is "" by default)
Ø If the " Text value of the field = InitialValue of the RequiredFieldValidator"
then it is not validated and the error text is made visible
- ErrorMessage: Message to display in a ValidationSummary control (eg "Your Name")
- Text: Message displayed in the RequiredFileValidator control when an error is encountered (eg "Name Required")
runat = "server"
ControlToValidate = "txtNom"
ErrorMessage = "Your name">
name is required
asp: RequiredFieldValidator>2 - RangeValidator:
Verify that the field value is ranged between two values.
This is when pressing the Send button that all fields will be checked and the error messages displayed
Dragging from tab validation of the toolbox
- ControlToValidate: ID of the control to validate / verify
- Type: the type of values tested (String, ...)
- MinimumValue: minimum value of the interval
- MaximumValue: Max interval
- ErrorMessage: Message to display in a ValidationSummary control (eg "Your Name")
- Text: Message displayed in the RangeValidator control when an error is encountered (eg "Name Required")
runat = "server"
ControlToValidate = "txtAge"
ErrorMessage = "invalid Age"
MaximumValue = "100"
MinimumValue = "0"
Type = "Integer">
invalid Age
asp: RangeValidator>3 - CompareValidator:
It compares the value of two fields so that they are exactly the same, otherwise the error message is raised
It is at validating event that the audit is performed and the error messages is displayed, and not when you press the Send button
Dragging from tab validation of the toolbox
- ControlToValidate: ID of the control to validate / verify
- ControlToCompare: ID of the control whose value is the basis (the value at which the control to validate must correspond if not an error message is displayed)
- ErrorMessage: Message to display in a ValidationSummary control (ex: " Email in the field confirmation Email")
- Text: Message displayed in the RangeValidator control when an error is encountered (eg: "The email entered in the confirmation field does not match ")
- ValueCompare: Allow to compare the value of a field relative to ValueCompare value, do not give ControlToCompare in this case but only the ControlToValidate
- Type: Data type of the field (String, ...)
runat = "server"
ControlToCompare = "txtEmail"
ControlToValidate = "txtConfirmationEmail"
ErrorMessage = "Email in the field Email confirmation ">
The email entered in the confirmation field does not match
asp: CompareValidator>4 - RegularExpressionValidator:
Compare the value of a field to a regular expression in order to validate it.
This is when you press the send button that all fields will be checked
and error messages will be displayed
Dragging from validation tab of the toolbox
- ControlToValidate: ID of the control to validate / verify
- ValidationExpression: regular expression of comparison (predefined or custom)
- ErrorMessage: Message to display in a ValidationSummary control (eg, "Email")
- Text: Message displayed in the RangeValidator control when an error is encountered (eg "Invalid Email")
runat = "server"
ControlToValidate = "txtEmail"
ErrorMessage = "Email"
ValidationExpression = "w + ([- +.] W +) * @ w + ([-.] W +) *. W + ([-.] W +) *">
invalid email
asp: RegularExpressionValidator>5 - CustomValidator:
Allow you to create custom validation, you can either use:
- Event ServerValidate (code-behind page)
- Or a JavaScript function through ClientValidationFunction attribute of the CustomValidator control
- Do not forget to assign ControlToValidate: control ID to verify
Drag a CustomValidator control on the page
Double click on the CustomValidator control then the CustomValidator1_ServerValidate event is generated in the code-behind of page
Example: check for a zip code field the length of the entryA - in the code-behind
protected void CustomValidator1_ServerValidate (object source, ServerValidateEventArgs args)
{
if (args.Value.Length> 5)
{
/ / Error messages of CustomValidator1 will be displayed
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
ControlToValidate = "txtCodePostal"
ErrorMessage = "Postcode" OnServerValidate = "CustomValidator1_ServerValidate">
Invalid postal code
asp: CustomValidator>B-or with a JavaScript function
<script type="text/javascript" language="javascript">
CustomValidator1_ServerValidate function (source, args)
{
if (args.Value.Length> 5)
{
/ / Error messages of CustomValidator1 will be displayed
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
/ / ->
script>
runat = "server"
ControlToValidate = "txtCodePostal"
ErrorMessage = "Postcode" ClientValidationFunction = "CustomValidator1_ServerValidate">
Invalid postal code
asp: CustomValidator>6 - ValidationSummary:
Allow to display such a summary errors encountered on the page ( List all posts of ErrorMessage properties of non validated controls)
7 - Me.Validate () or this.Validate ():
Allow to force the validation of the page programmatically (in code-behind page)
C # example:
protected void btnValidateForm_Click (object sender, EventArgs e)
{
this.Validate ();
if (this.IsValid == true)
{
lblResume.Text= this.txtNom.Text + "
" + this.txtEmail.Text;
}
}8 - ValidationGroup
Create validation groups in a form
Simply set the ValidationGroup property
FOR EACH CONTROLS (textbox, validation button, labels, validation controls)
of each group
(eg A for a group and B for another )
C # example:
/ / Example: we have 2 validation groups (A and B)
/ / Validate only Group A
this.Validate ("A");
if (this.IsValid == true)
{
Label1.Text = "Group A validated";;
}
/ / Validate the entire form (all groups)
this.Validate ();
if (this.IsValid == true)
{
Label1.Text = " Whole Form validated ";
}