Validate Mobile number using RegularExpressionValidator in ASP.Net

Last Reply 9 months ago By dharmendr

Posted 9 months ago

Please tell me validation coding for mobile number validation upto 10 digits. In case less or greater than 10 digit it must have to display as a invalid statement.

This is my registration coding page

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

<!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 Page</title>
</head>
<body background="images/238534-online-exam-background-e1426473359694.jpg">
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="X-Large" Style="z-index: 100;
            left: 531px; position: absolute; top: 33px" Text="REGISTRATION"></asp:Label>
        <asp:Label ID="Label2" runat="server" Font-Size="Large" Style="z-index: 101; left: 441px;
            position: absolute; top: 129px" Text="NAME:"></asp:Label>
        <asp:Label ID="Label3" runat="server" Font-Size="Large" Style="z-index: 102; left: 438px;
            position: absolute; top: 170px" Text="EMAIL ID:"></asp:Label>
        <asp:Label ID="Label4" runat="server" Font-Size="Large" Style="z-index: 103; left: 438px;
            position: absolute; top: 208px" Text="PASSWORD:"></asp:Label>
        <asp:Label ID="Label5" runat="server" Font-Size="Large" Style="z-index: 104; left: 436px;
            position: absolute; top: 247px" Text="CONFIRM PASSWORD:"></asp:Label>
        <asp:Label ID="Label6" runat="server" Font-Size="Large" Style="z-index: 105; left: 436px;
            position: absolute; top: 287px" Text="MOBILE NUMBER:"></asp:Label> // validation have to display
        <asp:Label ID="Label7" runat="server" Font-Size="Large" Style="z-index: 106; left: 435px;
            position: absolute; top: 325px" Text="COURSES:"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Style="z-index: 107; left: 676px; position: absolute;
            top: 123px"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" Style="z-index: 108; left: 676px; position: absolute;
            top: 166px"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" Style="z-index: 109; left: 676px; position: absolute;
            top: 202px" TextMode="Password" Width="150px"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server" Style="z-index: 110; left: 676px; position: absolute;
            top: 243px" TextMode="Password" Width="152px"></asp:TextBox>
        <asp:TextBox ID="TextBox5" runat="server" Style="z-index: 111; left: 677px; position: absolute;
            top: 283px"></asp:TextBox>
        <asp:DropDownList ID="DropDownList1" runat="server" Style="z-index: 112; left: 676px;
            position: absolute; top: 320px">
            <asp:ListItem>IBPS-PO</asp:ListItem>
            <asp:ListItem>IBPS-Clerk</asp:ListItem>
            <asp:ListItem>TNPSC</asp:ListItem>
            <asp:ListItem>VAO</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="z-index: 113;
            left: 469px; position: absolute; top: 392px" Text="REGISTER" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Style="z-index: 114;
            left: 679px; position: absolute; top: 388px" Text="CLEAR" />
        <asp:Label ID="Label8" runat="server" Font-Bold="True" Font-Size="Large" ForeColor="Maroon"
            Style="z-index: 115; left: 815px; position: absolute; top: 393px"></asp:Label>
        <asp:ImageButton ID="ImageButton1" runat="server" Height="53px" ImageUrl="~/images/111645-glowing-green-neon-icon-media-a-media31-back.png"
            OnClick="ImageButton1_Click" PostBackUrl="~/Default.aspx" Style="z-index: 116;
            left: 910px; position: absolute; top: 425px" Width="59px" />
        &nbsp;
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox2"
            ErrorMessage="Invalid Email-id" Style="z-index: 118; left: 857px; position: absolute;
            top: 165px" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox3"
            ControlToValidate="TextBox4" ErrorMessage="Both Passwords are not same" Height="21px"
            Style="z-index: 119; left: 860px; position: absolute; top: 244px" Width="186px"></asp:CompareValidator>
        &nbsp;
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="DropDownList1"
            ErrorMessage="fill the field" Style="z-index: 122; left: 863px; position: absolute;
            top: 322px"></asp:RequiredFieldValidator>
    
    </div>
    </form>
</body>
</html>

C# coding:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

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

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = String.Empty;
        TextBox2.Text = String.Empty;
        TextBox3.Text = String.Empty;
        TextBox4.Text = String.Empty;
        TextBox5.Text = String.Empty;
        Label8.Text = String.Empty;





    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=exam;Integrated Security=True");
        con.Open();
        String str="insert into reg(sname,semail,spass,sconfirm,smobile,scourse)values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','"+DropDownList1.Text+"')";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        cmd.Parameters.AddWithValue("sname", TextBox1.Text);
        cmd.Parameters.AddWithValue("semail", TextBox2.Text);
        cmd.Parameters.AddWithValue("spass", TextBox3.Text);
        cmd.Parameters.AddWithValue("sconfirm", TextBox4.Text);
        cmd.Parameters.AddWithValue("smobile", TextBox5.Text);
        cmd.Parameters.AddWithValue("scourse", DropDownList1.Text);
        Label8.Visible = true;
        Label8.Text = "Registered Successfully";
        String Display = "Registered Successfully";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + Display + "');", true);
        con.Close();
    }
}