Tuesday, 17 June 2014

Validation Controls,RequiredFieldValidator ,RangeValidator,RegularExpressionValidator,CompareValidator,CustomValidator,ValidationSummary



Validation Controls

RequiredFieldValidator  

The RequiredFieldValidator control ensures that the user does not skip an entry. The control fails validation if the value it contains does not change from its initial value when validation is performed. If all the fields in the page are valid, the page is valid.

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

<div style="text-align: left">
                                       First Name: <asp:TextBox ID="txtFirstname" runat="server"></asp:TextBox>

               <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                      ControlToValidate="txtFirstname" ErrorMessage="You no feed the Name !">

                </asp:RequiredFieldValidator>
       
<br />
                  Last Name:<asp:TextBox ID="txtLastname" runat="server" Wrap="False"></asp:TextBox>

                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                ControlToValidate="txtLastname" ErrorMessage="You no feed the Last Name !">

               </asp:RequiredFieldValidator>

 <br />
 <br />
                         <asp:Button ID="BtnSubmit" runat="server" Text="Submit"             onclick="BtnSubmit_Click" />
<br />
<br />
                             <asp:Label ID="lblSucceed" runat="server"></asp:Label>
</div>
</form>

protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        lblSucceed.Text = "You key already fully.";
    }



RangeValidator Control

The RangeValidator control tests whether an input value falls within a given range. RangeValidator uses three key properties to perform its validation: ControlToValidate contains the value to validate, MinimumValue defines the minimum value of the valid range, and MaximumValue defines the maximum value of the valid range. These constants are stored as string values, but are converted to the data type defined by Type when the comparison is performed

<form id = "form1" runat = "server">
<div>
                My age is:
                <asp:TextBox ID = "txtAge" runat = "server" MaxLength = "3" /> &nbsp;

                <asp:RangeValidator ID = "RangeValidator1" runat = "server" ControlToValidate = "txtAge" Type = "Integer"
                     ErrorMessage = "you must add age since , 25 arrive at 35 year."    MaximumValue = "35" MinimumValue = 30">
                </asp:RangeValidator>

</div>
</form>











RegularExpressionValidator Control

The RegularExpressionValidator control confirms that the entry matches a pattern defined by a regular expression. This type of validation allows you to check for predictable sequences of characters, such as those in social security numbers, e-mail addresses, telephone numbers, postal codes, and so on.


<form id="form1" runat="server">
<div>
                Your Email :<asp:TextBox ID="txtEmail" runat="server" />

                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"                                                                                                ControlToValidate="txtEmail"ErrorMessage="you add the email dishonestly !"
                                                            ValidationExpression="[\w]+@[\w]+\.(com|net|org|co\.th|go\.th|ac\.th|or\.th|go.\th)">
                </asp:RegularExpressionValidator>

<br /><br /><br />

                Enter a 5 digit zip code:      <asp:TextBox ID="txtCode" runat="server" />
                               
         <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"              
                 ControlToValidate="txtCode"    ErrorMessage="you put on a code the post dishonestly !"                            ValidationExpression="\d{5}">
                                </asp:RegularExpressionValidator>
<br />
                                <asp:Button ID="btnOK" runat="server" Text="Submit" onclick="btnOK_Click" />
<br />
                                <asp:Label ID="lblSum" runat="server"></asp:Label></div>

</form>

protected void btnOK_Click(object sender, EventArgs e)
    {
        lblSum.Text = "you key to are correct.";
    }


CompareValidator

CompareValidator uses three key properties to perform its validation. ControlToValidate and ControlToCompare contain the values to compare
<form id = "form1" runat = "server">
                <div style="text-align: left">
                                <p>fix the password that want.<br/>
                                                <asp:TextBox ID = "txtConfig" runat = "server" TextMode = "Password" />&nbsp;
                                </p>
                                <p>confirm the password.<br/>
                                <asp:TextBox ID = "txtConfirm" runat = "server" TextMode = "Password" />
                               
         <asp:CompareValidator ID = "CompareValidator1" runat = "server"
                                              ErrorMessage = "the password disagrees !" ControlToValidate = "txtConfirm"
                                              ControlToCompare = "txtConfig"></asp:CompareValidator>
                              </p>
                                <p><asp:Button ID = "btnOK" OnClick = "btnOK_Click" runat = "server" Text = "Submit" /></p>

                                <p><asp:Label ID = "lblSum" Text = "" runat = "server" /></p>
                </div>
</form>
   protected void btnOK_Click(object sender, EventArgs e)
    {
       lblSum.Text = "the password is correct.";
    }


CustomValidator Control

The CustomValidator control calls a user-defined function to perform validations that the standard validators can't handle. The custom function can execute on the server or in client-side script, such as JScript or VBScript. For client-side custom validation, the name of the custom function must be identified in the ClientValidationFunction property.





<head runat="server">
    <title>How to Use CustomValidator</title>
                                <script language = "JavaScript" type = "text/javascript">
                                                function validateNumber(value, arg) {
                                                                arg.IsValid = (arg.Value%5 == 0);
                                                }
                                </script>
</head>
<body>
<form id = "form1" runat = "server">
                                <div>
                                <p>the number that divide by 5 divisibl :
                                <asp:TextBox ID = "txtDigit" runat = "server" />
                               
                              <asp:CustomValidator ID = "CustomValidator1" runat = "server" ControlToValidate ="txtDigit"
                                                   ErrorMessage = "must add the number that divide by 5 divisible only."
                                                   ClientValidationFunction = "validateNumber">
                                </asp:CustomValidator>
                              </p>
                                <p><asp:Button ID = "btnOK" OnClick = "btnOK_Click" runat = "server" Text = "Submit" /></p>
                                <p><asp:Label ID = "lblSum" runat = "server" /></p>
                </div>
</form>
</body>
   protected void btnOK_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            lblSum.Text = "you add the number is correct.";
        }
    }


ValidationSummary Control

A ValidationSummary control is displayed when the IsValid property of the page is false. It "polls" each of the validation controls on the page and aggregates the text messages exposed by each. The following sample illustrates displaying errors with a ValidationSummary.

<asp:ValidationSummary ID = "ValidationSummary1" runat = "server"  HeaderText = "the all error that meet to is : ">
</asp:ValidationSummary>

No comments:

Post a Comment