0% found this document useful (0 votes)
5 views

Validators

The document discusses ASP.NET validation controls that ensure user input is valid before submission to the server, enhancing data integrity and security. It outlines two types of validation: client-side, which occurs in the browser to reduce server load, and server-side, which protects against malicious inputs. Various validator types are detailed, including RequiredFieldValidator, RangeValidator, RegularExpressionValidator, and CompareValidator, along with their attributes and usage in a sample ASP.NET form code.

Uploaded by

godsonsoban0342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Validators

The document discusses ASP.NET validation controls that ensure user input is valid before submission to the server, enhancing data integrity and security. It outlines two types of validation: client-side, which occurs in the browser to reduce server load, and server-side, which protects against malicious inputs. Various validator types are detailed, including RequiredFieldValidator, RangeValidator, RegularExpressionValidator, and CompareValidator, along with their attributes and usage in a sample ASP.NET form code.

Uploaded by

godsonsoban0342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

VALIDATORS

• When creating ASP.NET Web pages, facility should


be given to check whether the information entered
by the user is valid or not.

•ASP.NET provides a set of validation controls that


provide an easy-to-use and powerful techniques to
check for errors and, if necessary, display messages
to the user.

•ASP.NET validation controls validate the user input


data to ensure that useless, unauthenticated, or
contradictory data doesn’t enter the server
Two type of validation

Client side validation:

Validation done in the browser is called client-side validation


Checking the correction of data before it is passed to the server
Since all wrong inputs are checked at client side itself, server time is not wasted
Client Side validation does not require a round trip to the server, so the network traffic
will help the server to perform better.
If the user request does not require any server resources to validate the input , Client
Side Validation is used

Server side validation:


Validation done on the server is called server-side validation
The input submitted by the user is being sent to the server and validated using one of
server side scripting languages such as ASP.Net, PHP etc
It is better to validate user input on Server Side because it is used to protect against
the malicious users, who can easily bypass the Client Side scripting language and
submit dangerous input to the server.
TYPES OF VALIDATORS
• RequiredFieldValidator
• RangeValidator
• CompareValidator
• RegularExpressionValidator
• CustomValidator
• ValidationSummary
ATTRIBUTES OF ALL VALIDATORS
ATTRIBUTE MEANING
ControlToValidate Indicates the input control to validate.
Display Indicates how the error message is shown.
EnableClientScript Indicates whether client side validation will
take.
Enabled Enables or disables the validator.
ErrorMessage Indicates error string.
Text Error text to be shown if validation fails.
IsValid Indicates whether the value of the control is
valid.
SetFocusOnError It indicates whether in case of an invalid
control, the focus should switch to the
related input control.
ValidationGroup The logical group of multiple validators,
where this control belongs.
Validate() This method revalidates the control and
updates the IsValid property.
The RequiredFieldValidator control ensures
that the required field is not empty. It is
generally tied to a text box to force input into
the text box.

<asp:RequiredFieldValidator
ID=“ " runat="server"
ControlToValidate =“ "
ErrorMessage=“ “
InitialValue=“ ">
</asp:RequiredFieldValidator>
The RangeValidator control verifies that the
input value falls within a predetermined
range. It has three specific properties:

<asp:RangeValidator
ID=“ " runat="server"
ControlToValidate =“ "
ErrorMessage=“ “
Maximum=“ “ Mininum=“ “ Type=“”
</asp:RangeValidator>
The RegularExpressionValidator allows
validating the input text by matching against
a pattern of a regular expression. The regular
expression is set in the Validation Expression
property.

<asp:RegularExpressionValidator
ID=“ " runat="server"
ControlToValidate =“ "
ErrorMessage=“ “
ValidationExpression=“string">
</asp:RegularExpressionValidator>
The CompareValidator control compares a
value in one control with a fixed value or a
value in another control.
<asp:CompareValidator
ID=“ " runat="server"
ControlToCompare =“ control1 "
ValueToCompare=“control2 “
Operator=“ “
ErrorMessage=“ “

</asp:CompareValidator>
The ValidationSummary control does not perform any validation but
shows a summary of all errors in the page.

The summary displays the values of the ErrorMessage property of all


validation controls that failed validation.

Properties of Validation summary


ShowSummary : shows the error messages in specified format.
ShowMessageBox : shows the error messages in a separate window.

<asp:ValidationSummary
ID="ValidationSummary1“
runat="server"
DisplayMode = "BulletList/List/etc"
ShowSummary = "true/false“
HeaderText=“ " />
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Enter your name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Name is compulsory"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator><br />

<asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>


<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Age should be above 18 and below 50"
ControlToValidate="TextBox2" MaximumValue="50" MinimumValue="19"></asp:RangeValidator> <br >/>

<asp:Label ID="Label3" runat="server" Text="Mail id"></asp:Label>


<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox3"
ErrorMessage="Mail id wrong" ValidationExpression="\w+@\w+.com"></asp:RegularExpressionValidator><br />

<asp:Label ID="Label4" runat="server" Text="Password"></asp:Label>


<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text="Re enter Password"></asp:Label>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox5"
ControlToValidate="TextBox4" ErrorMessage="Password does not match"></asp:CompareValidator>

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Login“ /><br />

;<asp:Label ID="Label6“ runat="server" Text="Enter the details correctly to Login" Width="365px"></asp:Label></div>


</form>
</body>
</html>
Source code (.aspx.cs)
Design view ( .aspx)

You might also like