Visual C# Developer Center > Visual C# Forums > Visual C# Language > textbox validation for Phone number Validation
Ask a questionAsk a question
 

Answertextbox validation for Phone number Validation

  • Saturday, October 31, 2009 2:41 PMEmcube Challenges Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sir/Madam,

     I want to validate a textbox to accept a valid phone no

    It should have only numbers no +,(,),",",-   example it should not be like +91 - 9943243433
    It should not start with number less than 3.
    It should have only 10 numbers. No more or no less than 10 numbers.

    Thanking you
    EMCUBE

Answers

  • Wednesday, November 04, 2009 5:53 PMOmegaManMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    t should have only numbers no +,(,),",",-   example it should not be like +91 - 9943243433
    It should not start with number less than 3.
    It should have only 10 numbers. No more or no less than 10 numbers.

    One can use the match invalidator to verify specifics. Here I specify that the starting number cannot be 1 or 2 ( your less than 3 requirement):

    string pattern = @"^(?![012])(\d{10})$";
    
    HTH

    I discuss the match invalidator on my blog entitled: Regular Expression (Regex) Match Invalidator (?!) in .Net

    To start you on your Regular Expression Journey check out the .Net regular expression forum Regular Expressions and its informative top level post .Net Regex Resources Reference , which is a useful reference for beginners to experts.

    Also one can use this free tool (Expresso ) to test and learn about out regex patterns outside of ones .Net code.

    William Wegerson (www.OmegaCoder.Com )

All Replies

  • Saturday, October 31, 2009 2:55 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You can look at

    www.regexlib.com lotf of regular expressions there.

    For phone numbers you can visit. http://www.regexlib.com/Search.aspx?k=phone
  • Monday, November 02, 2009 5:40 AMBala.Nattarasan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    Hi,

    I hope thiss will help you.

    The solution for your problem is

    /^[3-9][0-9]{9}$/ - Stat with more than 2 and exactly 10 numbers.

  • Wednesday, November 04, 2009 3:54 PMSjums Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Tried this myself, it works. AND you cant type more than 10 chars into the textBox :)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace testapp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                textBoxPhoneNum.MaxLength = 10;
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                Regex validator = new Regex("(3|4|5|6|7|8|9){1}[0-9]{9}");
                string match = validator.Match(textBoxPhoneNum.Text).Value.ToString();
                if (match.Length == 10)
                {
                    label1.Text = "Valid";
                }
                else
                {
                    label1.Text = "Invalid";
                }
            }
        }
    }
    
    

    //Sjums
  • Wednesday, November 04, 2009 4:48 PMLouis.fr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Your Regex is more complex than needed

    (3|4|5|6|7|8|9){1}

    is exactly the same as

    [3-9]

    To verify that the match is done on the entire string, you can add ^ at the start and $ at the end.

    So, you can simply use Bala Nattarasan's regex:

    Regex validator = new Regex("^[3-9][0-9]{9}$");
    
    if(validator.Match(textBoxPhoneNum.Text).Success)
    {
        label1.Text = "Valid";
    }
    else
    {
        label1.Text = "Invalid";
    }
    
  • Wednesday, November 04, 2009 5:53 PMOmegaManMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    t should have only numbers no +,(,),",",-   example it should not be like +91 - 9943243433
    It should not start with number less than 3.
    It should have only 10 numbers. No more or no less than 10 numbers.

    One can use the match invalidator to verify specifics. Here I specify that the starting number cannot be 1 or 2 ( your less than 3 requirement):

    string pattern = @"^(?![012])(\d{10})$";
    
    HTH

    I discuss the match invalidator on my blog entitled: Regular Expression (Regex) Match Invalidator (?!) in .Net

    To start you on your Regular Expression Journey check out the .Net regular expression forum Regular Expressions and its informative top level post .Net Regex Resources Reference , which is a useful reference for beginners to experts.

    Also one can use this free tool (Expresso ) to test and learn about out regex patterns outside of ones .Net code.

    William Wegerson (www.OmegaCoder.Com )