You must Sign In to post a response.
  • Category: ASP.NET

    How to validate emailIdor mobile no using single textbox

    Hi,

    I need to validate emailId or Mobile no using single textbox from code behind . i am using vs 2010 asp.net c#.

    string patternForMobile = @"^\d{10}$";
    string pattern =@"\A(?:[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)\Z";

    bool isEmail = Regex.IsMatch(txtTabemail.Text.Trim(), pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    bool IsMobile = Regex.IsMatch(txtTabemail.Text.Trim(), patternForMobile, RegexOptions.IgnoreCase);
    if (!isEmail || !IsMobile)
    {
    pnlalert.Visible = true;
    if (false == isEmail)
    {
    lblalertmsg.Text = "Enter Valid Email ID..";
    lblalertmsg.ForeColor = System.Drawing.Color.Red;
    }
    else
    {

    lblalertmsg.Text = "Enter Valid Mobile No..";
    lblalertmsg.ForeColor = System.Drawing.Color.Red;
    }
    return;
    }

    Please help me to solve this
  • #761650
    Hello Ambika,

    Refer the below code :

    HTML Markup :

    <table>
    <tr>
    <td><b>Email or Mobile:</b></td>
    <td>
    <asp:TextBox ID="txtbox1" runat="server" />
    </td>
    </tr>
    <tr>
    <td></td>
    <td><asp:Button ID="btnValidate" runat="server" Text="Validate"
    onclick="btnValidate_Click" /> </td>
    </tr>
    </table>
    <asp:label id="lblerrormsg" runat="server" style=" font-weight:bold; " />


    Code - Behind :

    using System.Text.RegularExpressions;

    protected void btnValidate_Click(object sender, EventArgs e)
    {
    string txtval = txtbox1.Text;
    bool isMobile = Regex.IsMatch(txtbox1.Text.Trim(), @"^[789]\d{9}$");
    string Email = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*" + "@" + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$";
    if (txtbox1.Text.Length <= 9)
    {
    lblerrormsg.Text = "Invalid Email or Mobile";
    lblerrormsg.ForeColor = System.Drawing.Color.Red;
    }
    else
    {
    bool isValid = char.IsLetter(txtval.FirstOrDefault());
    if (isValid == true)
    {
    if (Regex.IsMatch(txtbox1.Text.Trim(), Email))
    {
    lblerrormsg.Text = "Valid Email";
    lblerrormsg.ForeColor = System.Drawing.Color.Green;
    }
    else
    {
    lblerrormsg.Text = "Invalid Email";
    lblerrormsg.ForeColor = System.Drawing.Color.Red;
    }
    }
    else
    {
    if (isMobile == true)
    {
    lblerrormsg.Text = "Valid Mobile";
    lblerrormsg.ForeColor = System.Drawing.Color.Green;
    }
    else
    {
    lblerrormsg.Text = "Invalid Mobile";
    lblerrormsg.ForeColor = System.Drawing.Color.Red;
    }
    }
    }
    }

    Hope this will help you.
    Let me know if still you are facing the problem with this code.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761657
    Hi,

    Here is the solution for you.

    Email validation:

    https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=validate%20email%20id%20in%20c%23

    Mobile Number Validation:

    https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=validate+mobile+number+in+c%23

    First off all you should search in Google, if you are unsuccessfully to get solution or any errors then you can raise it for help.

    Hope you understood...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments