C# .NET - How to validate phone no( ex; (123) 456-7890) in .Net?

Asked By Eswaran Radhakrishnan on 29-Sep-08 04:01 AM
Hi all,

I need to validate thd Phone no format is  "(123) 123-5678" length should be 14. I need to validate in .net windows application.

How to validate this in .net windows application?

Thanks
R. Eswaran.

I have tested this code - Perry replied to Eswaran Radhakrishnan on 29-Sep-08 04:03 AM

using System;
using System.Text.RegularExpressions;

namespace _11_RegularExpressions
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Regex phoneExp = new Regex( @"^\(\d{3}\)\s\d{3}-\d{4}$" );
string input;

Console.Write( "Enter a phone number: " );
input = Console.ReadLine();

while( phoneExp.Match( input ).Success == false )
{
Console.WriteLine( "Invalid input. Try again." );
Console.Write( "Enter a phone number: " );
input = Console.ReadLine();
}

Console.WriteLine( "Validated!" );
}
}
}

output of program - Perry replied to Eswaran Radhakrishnan on 29-Sep-08 04:11 AM

Enter a phone number: 1234
Invalid input. Try again.
Enter a phone number: (123)123-345
Invalid input. Try again.
Enter a phone number: (123)123-2345
Invalid input. Try again.
Enter a phone number: (123) 123-2345
Validated!

Please not the single space after (123).

re - Web Star replied to Eswaran Radhakrishnan on 29-Sep-08 05:17 AM

function isPhoneNumber(s)
{

     // Check for correct phone number
     rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);

     if (!rePhoneNumber.test(s)) {
          alert("Phone Number Must Be Entered As: (555) 555-1234");
          return false;
     }

return true;

re - Web Star replied to Eswaran Radhakrishnan on 29-Sep-08 05:20 AM

This is what your ValidationExpression property should look like:  

((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}( x\d{0,})?

 Copy and paste this string into the ValidationExpression property of your Regular Expression Validator control. What I did was add “( x\d{0,})?” to the end of the string (without the quotes) that Microsoft provided for us. My example allows a user to enter a phone number with an extension formatted as: (770)123-4567 x1234. If you prefer to use “ext” for extension, simply edit the above string and replace “x” with “ext”. Make sure that your database field is big enough to accommodate the entire phone number and extension – at least varchar(25) in our case.

use this - Web Star replied to Eswaran Radhakrishnan on 29-Sep-08 05:23 AM
To validate the phone number formats, use the following regular expression - \d{3}\-\d{8}|\d{8}

You can use the snippet at this link & adapt your code - http://quickstarts.asp.net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/ctrlref/validation/RegularExpressionValidator/RegExValidator1.src

Replace the ValidationExpression with the expression for phone number shown above, so that it looks like this -

<asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
      ControlToValidate="TextBox1"
      ValidationExpression="\d{3}\-\d{8}|\d{8}"
      Display="Static"
      Font-Names="verdana"
      Font-Size="10pt">
     Phone # must be of the format XXX-XXXXXXXX or XXXXXXXX
  </asp:RegularExpressionValidator>
working fine. How to restrict numbers in regex.? - Eswaran Radhakrishnan replied to Perry on 29-Sep-08 05:32 AM
end of post
Working fine. How to restrict Number in regex? - Eswaran Radhakrishnan replied to Perry on 29-Sep-08 05:35 AM
Hi,

Your code is working good. One more i want to do that I need to restrict number in every digit. How to do that?

for an example, I need to allow the first digit should be 0 to 2,
second digit should be 0 or 1. some thing like this

How to do aht?

Thanks
RE
solution - Perry replied to Eswaran Radhakrishnan on 29-Sep-08 07:45 AM
In this case you can speciies the number ranges like [1-3], [4-8] or you can specifies set of numbers by [1 2 4 5] so use following regex. Just put the ranges intead of \d.

Regex phoneExp = new Regex(@"^\([1-5]{3}\)\s\d{3}-\d{4}$"); will allow only digits from 1 to 5 at first three.

-Paresh
Thanks, I got it. - Eswaran Radhakrishnan replied to Perry on 29-Sep-08 11:04 AM
Thanks
I got it as I want.

Thanks
RE
great - Perry replied to Eswaran Radhakrishnan on 29-Sep-08 11:16 AM
enjoy..