Pages

Tuesday 23 April 2013

Write a program in C# to demonstrate the use of all the validation controls for a user registration page

Write a program in C# to demonstrate the use of all the validation controls for a user registration page

Program:

.aspx file:


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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table style="width: 100%;">
            <tr>
                <td>
                    <asp:Label ID="lblname" runat="server" Text="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                        ControlToValidate="txtname" ErrorMessage="please enter name" 
                        ValidationGroup="submit"></asp:RequiredFieldValidator>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" 
                        ErrorMessage="CustomValidator"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblemail" runat="server" Text="E-mail"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                        ControlToValidate="txtemail" ErrorMessage="please enter valid email" 
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
                        ValidationGroup="submit"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblpassword" runat="server" Text="Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtpassword" runat="server" 
                        TextMode="Password"></asp:TextBox>
                </td>
                <td>
                    <asp:CustomValidator ID="CustomValidator2" runat="server" 
                        ErrorMessage="password should be greater than 6" 
                        onservervalidate="CustomValidator2_ServerValidate" 
                        ControlToValidate="txtpassword" ValidationGroup="submit"></asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblrepass" runat="server" Text="Re-Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="lxlrepass" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <td>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" 
                        ControlToCompare="txtpassword" ControlToValidate="lxlrepass" 
                        ErrorMessage="passord is different" ValidationGroup="submit"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="submit" 
                        ValidationGroup="submit" />
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
                        ValidationGroup="submit" />
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class form : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length > 6)
        {
            args.IsValid = true;
        }
        else
            {
                args.IsValid = false;
        }

    }
}

Demo:


Write a program in C# to demonstrate the use of all the validation controls for a user registration page
Write a program in C# to demonstrate the use of all the validation controls for a user registration page



No comments:

Post a Comment