Search This Blog

Wednesday, October 5, 2011

VALIDATION CONTROLS IN ASP.NET


VALIDATION CONTROLS IN ASP.NET


In this article, we will consider the following controls in asp.net

·         RequiredField Validator,
·         Compare Validator
·         RangeValidator, and
·         Validation Summary

RequiredField Validator is used to inform the user if he leaves any of the data field(textbox , checkbox, dropdown ,password etc.) blank and submits the form.
CompareValidator compares data submitted in a field with pre-set value and report an error in case of a mis- match.

Range Validator checks if the value entered by the user falls within the specified range. If not, an error is reported.

Validation summary summarizes all the error messages and displays them  in a group. It is not a validation control as such but a reporter of error messages.

In this article example, the user leaves text blank, types 3000 in text 2 and types.
‘American’ in  text3. We get three error messages displayed  by the side of the respective input texts and also a summary report at the end of the form. A screen shot has been given.

Top of Form




ASP.NET VALIDATION CONTROLS
a) Requiredfeildvalidaror
b) Rangevalidator(5000 and 10000)
c) comparevalidator
=========================
Reqired Feild validator
name? you must enter a user name
_______________________
Range validator
Enter your salary ? you must enter a valid number
_________________________
comparevalidaror
enter your nationality [indian?]
dose not match
___________________________
validation summary
  • you must enter a user name
  • you must enter a valid number
  • dose not match

Bottom of Form


HTML    View
                <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Pagetitle>
head>
<body>
    <form id="form1" runat="server">
    <div>
ASP.NET VALIDATION CONTROLS
<br>
a) Requiredfeildvalidaror<br>
b) Rangevalidator(5000 and 10000)<br>
c) comparevalidator<br>
=========================<br>
   Reqired Feild validator <br>
  name? <asp:TextBox ID="text1" runat="server" >asp:TextBox>
  <asp:RequiredFieldValidator ID="rfv1" ErrorMessage="you must enter a user name" ControlToValidate="text1" runat="server">asp:RequiredFieldValidator>
<br>
 _______________________ <br>
  Range validator <br>
   Enter your salary ?
   <asp:Textbox ID="text2" runat="server">asp:Textbox>
   <asp:Rangevalidator
   id ="rv1"
   ErrorMessage = "you must enter a valid number"
   ControlToValidate= "text2"
   Minimumvalue ="5000"
   Maximumvalue ="10000"
   Type ="Integer" runat ="server">asp:Rangevalidator><br>
   _________________________<br>
   comparevalidaror <br>
   enter your nationality [indian?]<br>
    <asp:TextBox ID ="text3" runat="server">asp:TextBox>
    <asp:CompareValidator ID= "hjk" ErrorMessage = "dose not match" ValueToCompare ="indian" runat ="server" ControlToValidate="text3">asp:CompareValidator>
  
   <br>___________________________<br>
   <asp:Button OnClick ="job1" Text ="click" runat ="server" />
   validation summary <br>
   <asp:validationsummary ID ="gh" runat ="server" />
     div>
    form>
body>
html>

 Code behind part
protected void job1(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Response.Redirect
                ("default.aspx");
        }
    }

No comments :

Post a Comment