Le réseau pour les développeurs >
Forums - Accueil
>
Visual C# Language
>
textbox validation for Phone number Validation
textbox validation for Phone number Validation
- 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
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):
HTHstring pattern = @"^(?![012])(\d{10})$";
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 )- Proposé comme réponseHarry ZhuMSFT, Modérateurvendredi 6 novembre 2009 02:32
- Marqué comme réponseHarry ZhuMSFT, Modérateurlundi 9 novembre 2009 03:13
Toutes les réponses
- 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- Non proposé comme réponseOmegaManMVP, Modérateurmercredi 4 novembre 2009 17:29
- Proposé comme réponseTamer OzMVPmardi 3 novembre 2009 04:23
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.- Non proposé comme réponseOmegaManMVP, Modérateurmercredi 4 novembre 2009 17:28
- Proposé comme réponseBala.Nattarasan lundi 2 novembre 2009 05:41
- Proposé comme réponseBala.Nattarasan jeudi 5 novembre 2009 08:39
- 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 - 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"; }
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):
HTHstring pattern = @"^(?![012])(\d{10})$";
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 )- Proposé comme réponseHarry ZhuMSFT, Modérateurvendredi 6 novembre 2009 02:32
- Marqué comme réponseHarry ZhuMSFT, Modérateurlundi 9 novembre 2009 03:13

