ASP.NET validation controls define an important role in validating the user input data. Whenever the user gives the input, it must always be validated before sending it across to various layers of an application. If we get the user input with validation, then chances are that we are sending the wrong data. So, validation is a good idea to do whenever we are taking input from the user.
There are the following two types of validation in ASP.NET:
- Client-Side Validation
- Server-Side Validation
Client-Side Validation
When validation is done on the client browser, then it is known as Client-Side Validation. We use JavaScript to do the Client-Side Validation. For the last ten years, developers are using JavaScript for Client-Side Validation. JavaScript gives us full control over Client-Side Validation. Now, Microsoft is also embracing jQuery. Client-Side Validation is responsive and quick for end users, but not a secure form of validation. Client-Side Validation is faster, typically looks good, often association among messages and input.
Server-Side Validation
When validation occurs on the server, then its known as Server-Side Validation. Server-Side Validation is a secure form of validation. The main advantage of Server-Side Validation is that if the user somehow bypasses the Client-Side Validation, we can still catch the problem on server-side. Server-side provides more security and ensures that no invalid data is processed by the application. Server-Side Validation is done by writing the custom logic for validating all the input.
The following are the Validation Controls in ASP.NET:
- RequiredFieldValidator Control
- CompareValidator Control
- RangeValidator Control
- RegularExpressionValidator Control
- CustomFieldValidator Control
- ValidationSummary
Step 1
Start Visual Studio.
Step 2
Now, we need to create a website using "File" -> "New" -> "Website".
Step 3
Now, select the ASP.NET Empty Website and click on the OK button.
Step 4
Now, add the Web form by right-clicking on the website and provide a name to the Web Form.
I have given the page name as Validator.
Step 5
After the web form is created, we will design the form as shown in the following screenshot.
Step 6
We will write the following code in the Validator.aspx page.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style type="text/css">
- .auto-style1 {
- width: 100%;
- }
- .auto-style2 {
- width: 144px;
- }
- .auto-style3 {
- width: 144px;
- text-align: right;
- }
- .auto-style4 {
- width: 136px;
- }
- .auto-style5 {
- width: 144px;
- text-align: right;
- height: 26px;
- }
- .auto-style6 {
- width: 136px;
- height: 26px;
- }
- .auto-style7 {
- height: 26px;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <table class="auto-style1">
- <tr>
- <td class="auto-style3">User Name</td>
- <td class="auto-style4">
- <asp:TextBox ID="txtname" runat="server" Width="180px"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="Plz Enter UserName" ForeColor="Fuchsia"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td class="auto-style3">Enter Email</td>
- <td class="auto-style4">
- <asp:TextBox ID="txtemail" runat="server" Width="180px"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtemail" ErrorMessage="Enter EmailID" ForeColor="Fuchsia"></asp:RequiredFieldValidator>
- <br />
- <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtemail" ErrorMessage="Plz Enter Correct EmailID" ForeColor="Fuchsia" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
- </td>
- </tr>
- <tr>
- <td class="auto-style3">Enter Password</td>
- <td class="auto-style4">
- <asp:TextBox ID="txtpass" runat="server" Width="180px" TextMode="Password"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtpass" ErrorMessage="Plz Enter Password" ForeColor="Fuchsia"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td class="auto-style5">ReEnter Password</td>
- <td class="auto-style6">
- <asp:TextBox ID="txtrepass" runat="server" Width="180px" TextMode="Password"></asp:TextBox>
- </td>
- <td class="auto-style7">
- <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtrepass" ErrorMessage="ReEnter Password" ForeColor="Fuchsia"></asp:RequiredFieldValidator>
- <br />
- <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtpass" ControlToValidate="txtrepass" ErrorMessage="Password must me same" ForeColor="Fuchsia"></asp:CompareValidator>
- </td>
- </tr>
- <tr>
- <td class="auto-style3">Enter Full Name</td>
- <td class="auto-style4">
- <asp:TextBox ID="txtFn" runat="server" Width="180px"></asp:TextBox>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtFn" ErrorMessage="Plz Enter Full Name" ForeColor="Fuchsia"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td class="auto-style3">Select Your Country</td>
- <td class="auto-style4">
- <asp:DropDownList ID="ddlCity" runat="server" Width="187px">
- <asp:ListItem Value="0" Selected="True">--Select--</asp:ListItem>
- <asp:ListItem Value="1">India</asp:ListItem>
- <asp:ListItem Value="2">Australia</asp:ListItem>
- <asp:ListItem Value="3">America</asp:ListItem>
- <asp:ListItem Value="4">South Africa</asp:ListItem>
- <asp:ListItem Value="5">England</asp:ListItem>
- <asp:ListItem Value="6">Kenya</asp:ListItem>
- <asp:ListItem Value="7">WestIndies</asp:ListItem>
- </asp:DropDownList>
- </td>
- <td>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" InitialValue="0" ControlToValidate="ddlCity" ErrorMessage="Plz Select Your Country" ForeColor="Fuchsia"></asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style4"> </td>
- <td> </td>
- </tr>
- <tr>
- <td class="auto-style2"> </td>
- <td class="auto-style4">
- <asp:Button ID="Button1" runat="server" Text="Submit" ForeColor="#FF66FF" OnClick="Button1_Click" />
- </td>
- <td>
- <asp:Label ID="Label1" runat="server" Text="Label" Visible="false"></asp:Label>
- </td>
- </tr>
- </table>
- <div>
- </div>
- </form>
- <p>
- </p>
- </body>
- </html>
Step 7
Now, we need to execute the web form using the F5 key and will get the following window form.
I have also written the following code for a button click.
- protected void Button1_Click(object sender, EventArgs e)
- {
- Label1.Visible = true;
- Label1.Text = "All Validator working Perfectly!!!!!!";
- }
Step 8
Now, check whether the validation is working correctly or not. When we click on the submit button, then the following error can be seen.
The error shows that the textboxes can't be blank.
Step 9
Now, check the email validation on the web form. If we write the wrong email-id, then the error message appears on the form.
Step 10
Now, we need to check the password validation. If we are writing a different password into the password textboxes, then the error message appears on the web form.
Step 11
Now, we need to check the validation of the dropdown list. If we don't select the country name, then the following error message appears: "plz select your country name".
When all the textbooxes have been filled, select the country name. Now, on clicking the submit button, the following message appears.
No comments:
Post a Comment