Visual C# Developer Center >
Visual C# Forums
>
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
Answers
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 )- Proposed As Answer byHarry ZhuMSFT, ModeratorFriday, November 06, 2009 2:32 AM
- Marked As Answer byHarry ZhuMSFT, ModeratorMonday, November 09, 2009 3:13 AM
All Replies
- 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- Unproposed As Answer byOmegaManMVP, ModeratorWednesday, November 04, 2009 5:29 PM
- Proposed As Answer byTamer OzMVPTuesday, November 03, 2009 4:23 AM
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.- Unproposed As Answer byOmegaManMVP, ModeratorWednesday, November 04, 2009 5:28 PM
- Proposed As Answer byBala.Nattarasan Monday, November 02, 2009 5:41 AM
- Proposed As Answer byBala.Nattarasan Thursday, November 05, 2009 8:39 AM
- 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 )- Proposed As Answer byHarry ZhuMSFT, ModeratorFriday, November 06, 2009 2:32 AM
- Marked As Answer byHarry ZhuMSFT, ModeratorMonday, November 09, 2009 3:13 AM


