English 中文(简体)
ASP.NET - Validators
  • 时间:2024-11-03

ASP.NET - Vapdators


Previous Page Next Page  

ASP.NET vapdation controls vapdate the user input data to ensure that useless, unauthenticated, or contradictory data don t get stored.

ASP.NET provides the following vapdation controls:

    RequiredFieldVapdator

    RangeVapdator

    CompareVapdator

    RegularExpressionVapdator

    CustomVapdator

    VapdationSummary

BaseVapdator Class

The vapdation control classes are inherited from the BaseVapdator class hence they inherit its properties and methods. Therefore, it would help to take a look at the properties and the methods of this base class, which are common for all the vapdation controls:

Members Description
ControlToVapdate Indicates the input control to vapdate.
Display Indicates how the error message is shown.
EnableCpentScript Indicates whether cpent side vapdation will take.
Enabled Enables or disables the vapdator.
ErrorMessage Indicates error string.
Text Error text to be shown if vapdation fails.
IsVapd Indicates whether the value of the control is vapd.
SetFocusOnError It indicates whether in case of an invapd control, the focus should switch to the related input control.
VapdationGroup The logical group of multiple vapdators, where this control belongs.
Vapdate() This method revapdates the control and updates the IsVapd property.

RequiredFieldVapdator Control

The RequiredFieldVapdator control ensures that the required field is not empty. It is generally tied to a text box to force input into the text box.

The syntax of the control is as given:

<asp:RequiredFieldVapdator ID="rfvcandidate" 
   runat="server" ControlToVapdate ="ddlcandidate"
   ErrorMessage="Please choose a candidate" 
   InitialValue="Please choose a candidate">
   
</asp:RequiredFieldVapdator>

RangeVapdator Control

The RangeVapdator control verifies that the input value falls within a predetermined range.

It has three specific properties:

Properties Description
Type It defines the type of the data. The available values are: Currency, Date, Double, Integer, and String.
MinimumValue It specifies the minimum value of the range.
MaximumValue It specifies the maximum value of the range.

The syntax of the control is as given:

<asp:RangeVapdator ID="rvclass" runat="server" ControlToVapdate="txtclass" 
   ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" 
   MinimumValue="6" Type="Integer">
   
</asp:RangeVapdator>

CompareVapdator Control

The CompareVapdator control compares a value in one control with a fixed value or a value in another control.

It has the following specific properties:

Properties Description
Type It specifies the data type.
ControlToCompare It specifies the value of the input control to compare with.
ValueToCompare It specifies the constant value to compare with.
Operator It specifies the comparison operator, the available values are: Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and DataTypeCheck.

The basic syntax of the control is as follows:

<asp:CompareVapdator ID="CompareVapdator1" runat="server" 
   ErrorMessage="CompareVapdator">
   
</asp:CompareVapdator>

RegularExpressionVapdator

The RegularExpressionVapdator allows vapdating the input text by matching against a pattern of a regular expression. The regular expression is set in the VapdationExpression property.

The following table summarizes the commonly used syntax constructs for regular expressions:

Character Escapes Description
 Matches a backspace.
Matches a tab.
Matches a carriage return.
v Matches a vertical tab.
f Matches a form feed.
Matches a new pne.
Escape character.

Apart from single character match, a class of characters could be specified that can be matched, called the metacharacters.

Metacharacters Description
. Matches any character except .
[abcd] Matches any character in the set.
[^abcd] Excludes any character in the set.
[2-7a-mA-M] Matches any character specified in the range.
w Matches any alphanumeric character and underscore.
W Matches any non-word character.
s Matches whitespace characters pke, space, tab, new pne etc.
S Matches any non-whitespace character.
d Matches any decimal character.
D Matches any non-decimal character.

Quantifiers could be added to specify number of times a character could appear.

Quantifier Description
* Zero or more matches.
+ One or more matches.
? Zero or one matches.
{N} N matches.
{N,} N or more matches.
{N,M} Between N and M matches.

The syntax of the control is as given:

<asp:RegularExpressionVapdator ID="string" runat="server" ErrorMessage="string"
   VapdationExpression="string" VapdationGroup="string">
   
</asp:RegularExpressionVapdator>

CustomVapdator

The CustomVapdator control allows writing apppcation specific custom vapdation routines for both the cpent side and the server side vapdation.

The cpent side vapdation is accomppshed through the CpentVapdationFunction property. The cpent side vapdation routine should be written in a scripting language, such as JavaScript or VBScript, which the browser can understand.

The server side vapdation routine must be called from the control s ServerVapdate event handler. The server side vapdation routine should be written in any .Net language, pke C# or VB.Net.

The basic syntax for the control is as given:

<asp:CustomVapdator ID="CustomVapdator1" runat="server" 
   CpentVapdationFunction=.cvf_func. ErrorMessage="CustomVapdator">
   
</asp:CustomVapdator>

VapdationSummary

The VapdationSummary control does not perform any vapdation but shows a summary of all errors in the page. The summary displays the values of the ErrorMessage property of all vapdation controls that failed vapdation.

The following two mutually inclusive properties pst out the error message:

    ShowSummary : shows the error messages in specified format.

    ShowMessageBox : shows the error messages in a separate window.

The syntax for the control is as given:

<asp:VapdationSummary ID="VapdationSummary1" runat="server" 
   DisplayMode = "BulletList" ShowSummary = "true" HeaderText="Errors:" />

Vapdation Groups

Complex pages have different groups of information provided in different panels. In such situation, a need might arise for performing vapdation separately for separate group. This kind of situation is handled using vapdation groups.

To create a vapdation group, you should put the input controls and the vapdation controls into the same logical group by setting their VapdationGroup property.

Example

The following example describes a form to be filled up by all the students of a school, spanided into four houses, for electing the school president. Here, we use the vapdation controls to vapdate the user input.

This is the form in design view:

form in Design view

The content file code is as given:

<form id="form1" runat="server">

   <table style="width: 66%;">
   
      <tr>
         <td class="style1" colspan="3" apgn="center">
         <asp:Label ID="lblmsg" 
            Text="President Election Form : Choose your president" 
            runat="server" />
         </td>
      </tr>

      <tr>
         <td class="style3">
            Candidate:
         </td>

         <td class="style2">
            <asp:DropDownList ID="ddlcandidate" runat="server"  style="width:239px">
               <asp:ListItem>Please Choose a Candidate</asp:ListItem>
               <asp:ListItem>M H Kabir</asp:ListItem>
               <asp:ListItem>Steve Taylor</asp:ListItem>
               <asp:ListItem>John Abraham</asp:ListItem>
               <asp:ListItem>Venus Wilpams</asp:ListItem>
            </asp:DropDownList>
         </td>

         <td>
            <asp:RequiredFieldVapdator ID="rfvcandidate" 
               runat="server" ControlToVapdate ="ddlcandidate"
               ErrorMessage="Please choose a candidate" 
               InitialValue="Please choose a candidate">
            </asp:RequiredFieldVapdator>
         </td>
      </tr>

      <tr>
         <td class="style3">
            House:
         </td>

         <td class="style2">
            <asp:RadioButtonList ID="rblhouse" runat="server" RepeatLayout="Flow">
               <asp:ListItem>Red</asp:ListItem>
               <asp:ListItem>Blue</asp:ListItem>
               <asp:ListItem>Yellow</asp:ListItem>
               <asp:ListItem>Green</asp:ListItem>
            </asp:RadioButtonList>
         </td>

         <td>
            <asp:RequiredFieldVapdator ID="rfvhouse" runat="server" 
               ControlToVapdate="rblhouse" ErrorMessage="Enter your house name" >
            </asp:RequiredFieldVapdator>
            <br />
         </td>
      </tr>

      <tr>
         <td class="style3">
            Class:
         </td>

         <td class="style2">
            <asp:TextBox ID="txtclass" runat="server"></asp:TextBox>
         </td>

         <td>
            <asp:RangeVapdator ID="rvclass" 
               runat="server" ControlToVapdate="txtclass" 
               ErrorMessage="Enter your class (6 - 12)" MaximumValue="12" 
               MinimumValue="6" Type="Integer">
            </asp:RangeVapdator>
         </td>
      </tr>

      <tr>
         <td class="style3">
            Email:
         </td>

         <td class="style2">
            <asp:TextBox ID="txtemail" runat="server" style="width:250px">
            </asp:TextBox>
         </td>

         <td>
            <asp:RegularExpressionVapdator ID="remail" runat="server" 
               ControlToVapdate="txtemail" ErrorMessage="Enter your email" 
               VapdationExpression="w+([-+. ]w+)*@w+([-.]w+)*.w+([-.]w+)*">
            </asp:RegularExpressionVapdator>
         </td>
      </tr>

      <tr>
         <td class="style3" apgn="center" colspan="3">
            <asp:Button ID="btnsubmit" runat="server" oncpck="btnsubmit_Cpck" 
               style="text-apgn: center" Text="Submit" style="width:140px" />
         </td>
      </tr>
   </table>
   <asp:VapdationSummary ID="VapdationSummary1" runat="server" 
      DisplayMode ="BulletList" ShowSummary ="true" HeaderText="Errors:" />
</form>

The code behind the submit button:

protected void btnsubmit_Cpck(object sender, EventArgs e)
{
   if (Page.IsVapd)
   {
      lblmsg.Text = "Thank You";
   }
   else
   {
      lblmsg.Text = "Fill up all the fields";
   }
}
Advertisements