Windows Develop Bookmark and Share   
 index > Windows Forms Designer > What is Regular Expression for US Phone Number(Windows application)?
 

What is Regular Expression for US Phone Number(Windows application)?

I have US phone number patternlike 111-111-1111 in text box. How can i validate it? And also some times i have phone number like 11-111-111-1111 . How can igive validation for this in C#(Windows application)?

Thanks in advance
Ganapatisb  Tuesday, September 29, 2009 9:13 AM
You can find a very good library of Regex here





Please mark as Answer/ Helpful, if it helps you.
Hassan Mehmood  Tuesday, September 29, 2009 10:58 AM

Hi Ganapatisb,

These are the regular expressions:
111-111-1111: ^\d{3}-\d{3}-\d{4}$
11-111-111-1111 : ^\d{2}-\d{3}-\d{3}-\d{4}$

The code snippet below shows how to use them:
private bool IsValid(string input)

{

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(

@"^\d{3}-\d{3}-\d{4}$", System.Text.RegularExpressions.RegexOptions.Compiled);

if (reg.Match(input).Success)

return true;

else

return false;

}

You can get more about regular expressions from:
http://msdn.microsoft.com/en-us/library/2k3te2cs(VS.80).aspx
http://www.codeproject.com/KB/string/re.aspx

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 30, 2009 10:43 AM
You can find a very good library of Regex here





Please mark as Answer/ Helpful, if it helps you.
Hassan Mehmood  Tuesday, September 29, 2009 10:58 AM

Hi Ganapatisb,

These are the regular expressions:
111-111-1111: ^\d{3}-\d{3}-\d{4}$
11-111-111-1111 : ^\d{2}-\d{3}-\d{3}-\d{4}$

The code snippet below shows how to use them:
private bool IsValid(string input)

{

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(

@"^\d{3}-\d{3}-\d{4}$", System.Text.RegularExpressions.RegexOptions.Compiled);

if (reg.Match(input).Success)

return true;

else

return false;

}

You can get more about regular expressions from:
http://msdn.microsoft.com/en-us/library/2k3te2cs(VS.80).aspx
http://www.codeproject.com/KB/string/re.aspx

Let me know if this helps or not.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 30, 2009 10:43 AM
Hi Aland,

Its working fine. Thanks a lot.
Ganapatisb  Thursday, October 01, 2009 12:35 PM

You can use google to search for other answers

Custom Search

More Threads

• Loading ToolstripButton Images from another component ( rather than from Resource )
• WYSIWYG XHTML Editor
• Designer duplicates resources used from an external project?
• Collection Of Playing Cards On UserControl
• How to add controls to the designer surface during run time
• Passing Checkbox.Text if checked
• ListView ScrollBar
• DefaultValue attribute VC++ definition not working
• Powerpoint in a Visual Basic.Net Form
• Source Code Not Updating on ExpandableObject Converter