Custom validator - DotNet and DataBase

Saturday, 18 August 2018

Custom validator



The custom validator is to handle the restriction logic which is not possible with the remaining validation controls. For example, user registration form should contain at least one upper case letter on Username field. For that, we need to write our own logic to check the username.


Example
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RequiredFieldvalidator.aspx.cs" Inherits="WebProgramming.Validators.RequiredFieldvalidator" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>
                      UserName:  <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="UserName is required." ControlToValidate="txtUserName"></asp:RequiredFieldValidator></td>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Username should have atleast a capital letter" ControlToValidate="txtUserName" OnServerValidate="CustomValidator1_ServerValidate"  SetFocusOnError="True"
                        ></asp:CustomValidator>
                </tr>
                 <tr>
                    <td>
                      Password:       <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Password is required." ControlToValidate="txtPassword"></asp:RequiredFieldValidator></td>
                </tr>
                 <tr>
                    <td>
                      Confirm password:       <asp:TextBox ID="txtConfirmvalidator" runat="server"  TextMode="Password"></asp:TextBox><asp:RequiredFieldValidator ID="RangeValidator2" runat="server" ErrorMessage="Confirm password is required." ControlToValidate="txtConfirmvalidator" MinimumValue="18" MaximumValue="35"></asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="cvConfirmpassword" runat="server" ControlToValidate="txtConfirmvalidator" ControlToCompare="txtPassword" ErrorMessage="Password mismatch"></asp:CompareValidator>
                    </td>
                </tr>
                 <tr>
                    <td>
                      Age:       <asp:TextBox ID="txtAge" runat="server"></asp:TextBox><asp:RangeValidator ID="rfvAge" runat="server" ErrorMessage="User age should be with in 18-35." ControlToValidate="txtAge" MinimumValue="18" MaximumValue="35"></asp:RangeValidator></td>
                </tr>

                  <tr>
                    <td>
                      Email:       <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><asp:RegularExpressionValidator ID="RangeValidator1" runat="server" ErrorMessage="Supplied email is not in correct format." ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td>
                </tr>

                <tr>
                    <td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit"  /></td>
                </tr>
            </table>
        </div>
    </form>
</body>

</html>


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

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

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string username = txtUserName.Text.ToLower();

            if (txtUserName.Text == username)
            {
                args.IsValid = false;
            }
        }
    }
}

Output



No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.