Validation Controls
Prepared by
Rajani Khushal K
Introduction
• Validating a form is most important part of
Website Development.
• User enters data of his or her choice; you
need that entered data in specific format.
• Initially for validating controls we used to
write JavaScript’s.
Introduction
• But it took more number of lines of code
and more time.
• With Validation controls this could be done
in couple of minutes.
Types of Validation
• Client Side Validation:
– It means the checking of data format or
validation is done at Client Side.
– It is done by the browser itself.
– The Client Side validation gives faster
response to Client.
Types of Validation
• Server Side Validation:
– It means the checking of data format or
validation is carried out at Server Side.
– Even though the data is checked at client
side, the data is again validated at Server
Side, so that the attacker or hacker cannot
manipulate the data on the browser side.
What are Validation Controls?
• The Controls that are used to validate the
data against some input format are known
as Validation Controls.
• If the data does not pass the validation, it
will display an error message to the user
and he would be able to submit the data.
Different Types of Validation
Controls
• RequiredFieldValidator
• CompareValidator
• CustomValidator
• RangeValidator
• RegularExpressionValidator
• ValidationSummary
RequiredFieldValidator Control
• The RequiredFieldValidator control is used to
make an input control a required field.
• With this control, the validation fails if the input
value does not change from its initial value. By
default, the initial value is an empty string ("").
• The basic syntax is given as:
– <asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="This
field can not be blank.">
</asp:RequiredFieldValidator>
CompareValidator Controls
• 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.
• The basic syntax for controls is:
– <asp:CompareValidator ID="CompareText"
runat="server"
ControlToCompare="TextBox1"
ControlToValidate="TextBox2"
ErrorMessage="Test Does not match" />
RangeValidator Controls
• 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.
• The basic syntax is given as:
– <asp:RangeValidator ID="Validator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Please enter no between 20 to 40."
MaximumValue="40" MinimumValue="20“
Type="Integer"></asp:RangeValidator>
RegularExpressionValidator
Control
• The RegularExpressionValidator control is
used to ensure that an input value
matches a specified pattern.
• It is powerful tool which is used to check
against specific types of values which may
be string, integer, data, mixed etc.
• For example:
– Input Mobile no in a specific format
– To input zip code of 6 digits only.
– To input Email address
RegularExpressionValidator
Symbols
Character Description
w Any word character (letter, number or underscore)
d Any digit
D Any character that is not Digit
s Any white space character like tab or space
S Any non white space character
| To specify one of the given choices
[ ] To specify a range of character or set of characters
[^ ] To specify opposite of specified characters.
* To specify zero or more number of characters
+ To specify one or more number of characters
RegularExpressionValidator
Symbols
Expression Meaning
w{10} Allow only 0 character which can be Letter, Number or
Underscore
w{5,10} Allows character between length 5 to 10
w{5,} Allows characters with minimum length 5
d{5} Allows only 5 digits
[M | m | F | f] Allows one of M, m, F or F
[A - D] Allows any one between A to D
[^A - D]{5} Allows 5 characters except from A to D
[A - D]* Allows zero or any no of characters between A to D
S{10} Allows 10 non white space characters
RegularExpressionValidator
Symbols
Use Example
Zip Code 361002
Mobile No. +91 - 9999999999
Email test@gmail.com
Product code B- 2345
Choice Y or y or N or n
Symbol
d {6}
+d{2} - d{10}
S+@S+.S+
[A-D] - d{4}
[Y | y | N | n]
• The syntax can be given as:
– <asp:RegularExpressionValidator
ControlToValidate="txtbox1"
ValidationExpression="d{6}"
EnableClientScript="false"
ErrorMessage="The zip code must be 5
numeric digits!"
runat="server" />
CustomValidator Controls
• The CustomValidator control allows you to
write a method to handle the validation of
the value entered.
• CustomValidator control allows us to
specify our own functions:
– Server Side Validation Functions
– Client Side Validation Functions
CustomValidator Controls
• Server side Validation Function:
– It is executed on server side.
– The name of the server side function is
ServerValidate function.
– The function name is automatically added
after the control name.
CustomValidator Controls
• Server side Validation Function:
– For example:
protected void cusCustom_ServerValidate(object o,
ServerValidateEventArgs e)
{
if (e.Value.Length == 8)
e.IsValid = true;
else
e.IsValid = false;
}
CustomValidator Controls
• Server side Validation Function:
– It has two main arguments:
– Object Source:
• Source name of control.
– ServerValidateEventArgs e:
• It will give us two important properties about the
control.
– e.Value
» This will give us value of control
– e.IsValid
» It can set to true or false. It depends on the choice of
checking.
CustomValidator Controls
• Client Side Validation Function:
– This is used to perform client side validation
function name.
– It has two property:
• ClientValidationFunction
• ValidateEmptytext
CustomValidator Controls
• Client Side Validation Function:
<script language="JavaScript">
function validateNumber(oSrc, args) {
args.IsValid = (args.Value % 5 == 0);
}
</script>
CustomValidator Controls
• Server Side Validate Function will be
checked when its value will be submitted
to the server.
• While Client Side Validate Function will be
checked as soon as control loses focus.
CustomValidator Controls
• The syntax can be given as:
– <asp:CustomValidator
ControlToValidate="txt1"
ClientValidationFunction ="user“
OnServerValidatie=“test_ServerVali”
Text="A username must be between 8 and 16
characters!"
runat="server"/>
ValidationSummary Controls
• The ValidationSummary control is used to
display a summary of all validation errors
occurred in a Web page.
• 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.
ValidationSummary Controls
• It has four property:
– DisplayMode: Mode of displaying all the
errors. It can be List, BulletList or single
Paragraph.
– HeaderText: We can give the header of
validation summary.
– ShowMessageBox: It is Boolean property
which is used to see error list as MessageBox
– ShowSummary: It is Boolean property which
is used to see error list as Summary.
• The syntax can be given as:
– <asp:ValidationSummary
ID="ValidationSummary1"
HeaderText="You must enter a value in the
following fields:"
ShowMessageBox="true"
ShowSummary="false"
EnableClientScript="true"
runat="server"/>
Validation Group
• Validation Group is a new feature that was
included in ASP.NET 2.0 version.
• The validation group helps you to group
the controls of a single page into separate
groups, so that you can submit each group
separately.
• Each group will have separate validation
controls.
Validation Group
• When you submit a group of values from a
group, only the validation controls which
are intended for that group would be
executed.
• This grouping of controls is achieved in
ASP.Net 2.0 by modifying the property
called the ValidationGroup property.

validation-controls.pdf ioue8n uoh souu o3i

  • 1.
  • 2.
    Introduction • Validating aform is most important part of Website Development. • User enters data of his or her choice; you need that entered data in specific format. • Initially for validating controls we used to write JavaScript’s.
  • 3.
    Introduction • But ittook more number of lines of code and more time. • With Validation controls this could be done in couple of minutes.
  • 4.
    Types of Validation •Client Side Validation: – It means the checking of data format or validation is done at Client Side. – It is done by the browser itself. – The Client Side validation gives faster response to Client.
  • 5.
    Types of Validation •Server Side Validation: – It means the checking of data format or validation is carried out at Server Side. – Even though the data is checked at client side, the data is again validated at Server Side, so that the attacker or hacker cannot manipulate the data on the browser side.
  • 6.
    What are ValidationControls? • The Controls that are used to validate the data against some input format are known as Validation Controls. • If the data does not pass the validation, it will display an error message to the user and he would be able to submit the data.
  • 7.
    Different Types ofValidation Controls • RequiredFieldValidator • CompareValidator • CustomValidator • RangeValidator • RegularExpressionValidator • ValidationSummary
  • 8.
    RequiredFieldValidator Control • TheRequiredFieldValidator control is used to make an input control a required field. • With this control, the validation fails if the input value does not change from its initial value. By default, the initial value is an empty string (""). • The basic syntax is given as: – <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="This field can not be blank."> </asp:RequiredFieldValidator>
  • 9.
    CompareValidator Controls • TheCompareValidator control is used to compare the value of one input control to the value of another input control or to a fixed value. • The basic syntax for controls is: – <asp:CompareValidator ID="CompareText" runat="server" ControlToCompare="TextBox1" ControlToValidate="TextBox2" ErrorMessage="Test Does not match" />
  • 10.
    RangeValidator Controls • TheRangeValidator 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. • The basic syntax is given as: – <asp:RangeValidator ID="Validator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please enter no between 20 to 40." MaximumValue="40" MinimumValue="20“ Type="Integer"></asp:RangeValidator>
  • 11.
    RegularExpressionValidator Control • The RegularExpressionValidatorcontrol is used to ensure that an input value matches a specified pattern. • It is powerful tool which is used to check against specific types of values which may be string, integer, data, mixed etc. • For example: – Input Mobile no in a specific format – To input zip code of 6 digits only. – To input Email address
  • 12.
    RegularExpressionValidator Symbols Character Description w Anyword character (letter, number or underscore) d Any digit D Any character that is not Digit s Any white space character like tab or space S Any non white space character | To specify one of the given choices [ ] To specify a range of character or set of characters [^ ] To specify opposite of specified characters. * To specify zero or more number of characters + To specify one or more number of characters
  • 13.
    RegularExpressionValidator Symbols Expression Meaning w{10} Allowonly 0 character which can be Letter, Number or Underscore w{5,10} Allows character between length 5 to 10 w{5,} Allows characters with minimum length 5 d{5} Allows only 5 digits [M | m | F | f] Allows one of M, m, F or F [A - D] Allows any one between A to D [^A - D]{5} Allows 5 characters except from A to D [A - D]* Allows zero or any no of characters between A to D S{10} Allows 10 non white space characters
  • 14.
    RegularExpressionValidator Symbols Use Example Zip Code361002 Mobile No. +91 - 9999999999 Email test@gmail.com Product code B- 2345 Choice Y or y or N or n Symbol d {6} +d{2} - d{10} S+@S+.S+ [A-D] - d{4} [Y | y | N | n]
  • 15.
    • The syntaxcan be given as: – <asp:RegularExpressionValidator ControlToValidate="txtbox1" ValidationExpression="d{6}" EnableClientScript="false" ErrorMessage="The zip code must be 5 numeric digits!" runat="server" />
  • 16.
    CustomValidator Controls • TheCustomValidator control allows you to write a method to handle the validation of the value entered. • CustomValidator control allows us to specify our own functions: – Server Side Validation Functions – Client Side Validation Functions
  • 17.
    CustomValidator Controls • Serverside Validation Function: – It is executed on server side. – The name of the server side function is ServerValidate function. – The function name is automatically added after the control name.
  • 18.
    CustomValidator Controls • Serverside Validation Function: – For example: protected void cusCustom_ServerValidate(object o, ServerValidateEventArgs e) { if (e.Value.Length == 8) e.IsValid = true; else e.IsValid = false; }
  • 19.
    CustomValidator Controls • Serverside Validation Function: – It has two main arguments: – Object Source: • Source name of control. – ServerValidateEventArgs e: • It will give us two important properties about the control. – e.Value » This will give us value of control – e.IsValid » It can set to true or false. It depends on the choice of checking.
  • 20.
    CustomValidator Controls • ClientSide Validation Function: – This is used to perform client side validation function name. – It has two property: • ClientValidationFunction • ValidateEmptytext
  • 21.
    CustomValidator Controls • ClientSide Validation Function: <script language="JavaScript"> function validateNumber(oSrc, args) { args.IsValid = (args.Value % 5 == 0); } </script>
  • 22.
    CustomValidator Controls • ServerSide Validate Function will be checked when its value will be submitted to the server. • While Client Side Validate Function will be checked as soon as control loses focus.
  • 23.
    CustomValidator Controls • Thesyntax can be given as: – <asp:CustomValidator ControlToValidate="txt1" ClientValidationFunction ="user“ OnServerValidatie=“test_ServerVali” Text="A username must be between 8 and 16 characters!" runat="server"/>
  • 24.
    ValidationSummary Controls • TheValidationSummary control is used to display a summary of all validation errors occurred in a Web page. • 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.
  • 25.
    ValidationSummary Controls • Ithas four property: – DisplayMode: Mode of displaying all the errors. It can be List, BulletList or single Paragraph. – HeaderText: We can give the header of validation summary. – ShowMessageBox: It is Boolean property which is used to see error list as MessageBox – ShowSummary: It is Boolean property which is used to see error list as Summary.
  • 26.
    • The syntaxcan be given as: – <asp:ValidationSummary ID="ValidationSummary1" HeaderText="You must enter a value in the following fields:" ShowMessageBox="true" ShowSummary="false" EnableClientScript="true" runat="server"/>
  • 27.
    Validation Group • ValidationGroup is a new feature that was included in ASP.NET 2.0 version. • The validation group helps you to group the controls of a single page into separate groups, so that you can submit each group separately. • Each group will have separate validation controls.
  • 28.
    Validation Group • Whenyou submit a group of values from a group, only the validation controls which are intended for that group would be executed. • This grouping of controls is achieved in ASP.Net 2.0 by modifying the property called the ValidationGroup property.