Le réseau pour les développeurs > Forums - Accueil > Visual C# Language > textbox validation for Phone number Validation
Poser une questionPoser une question
 

Traitéetextbox validation for Phone number Validation

  • samedi 31 octobre 2009 14:41Emcube Challenges Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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

Réponses

Toutes les réponses

  • samedi 31 octobre 2009 14:55Tamer OzMVPMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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
  • lundi 2 novembre 2009 05:40Bala.Nattarasan Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Réponse proposée
    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.

  • mercredi 4 novembre 2009 15:54Sjums Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du 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
  • mercredi 4 novembre 2009 16:48Louis.fr Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du 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";
    }
    
  • mercredi 4 novembre 2009 17:53OmegaManMVP, ModérateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du 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 )