Current Forum Contest Rankings
[)ia6l0 iii - $229  Nasa Ares I-X - $81  mv ark - $55  Bill Look - $35  egg egg - $16  
Jonathan VH - $191  F Cali - $55  Web star - $51  web mavin - $21  Venkata K - $15  
Twitter Rss Feeds
search

  View Other C# .NET Posts   Ask New Question  Ask New Question With Power Editor

How to validate phone no( ex; (123) 456-7890) in .Net?
Eswaran Radhakrishnan posted at Monday, September 29, 2008 4: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.

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0
I have tested this code
Raj Cool... provided a rated reply to Eswaran Radhakrishnan on Monday, September 29, 2008 4: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!" );
}
}
}
Reply    Reply Using Power Editor
  Rank Winnings Points
November 32 $0.00 6
October 0 $0.00 0

output of program
Raj Cool... replied to Eswaran Radhakrishnan on Monday, September 29, 2008 4: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).
Reply    Reply Using Power Editor
  Rank Winnings Points
November 32 $0.00 6
October 0 $0.00 0

re
Web star replied to Eswaran Radhakrishnan on Monday, September 29, 2008 5: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;

Reply    Reply Using Power Editor
  Rank Winnings Points
November 6 $51.00 167
October 10 $28.00 94

re
Web star replied to Eswaran Radhakrishnan on Monday, September 29, 2008 5: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.

Reply    Reply Using Power Editor
  Rank Winnings Points
November 6 $51.00 167
October 10 $28.00 94

use this
Web star replied to Eswaran Radhakrishnan on Monday, September 29, 2008 5: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>
Reply    Reply Using Power Editor
  Rank Winnings Points
November 6 $51.00 167
October 10 $28.00 94

working fine. How to restrict numbers in regex.?
Eswaran Radhakrishnan replied to Raj Cool... on Monday, September 29, 2008 5:32 AM

end of post
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

Working fine. How to restrict Number in regex?
Eswaran Radhakrishnan replied to Raj Cool... on Monday, September 29, 2008 5: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
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

solution
Raj Cool... provided a rated reply to Eswaran Radhakrishnan on Monday, September 29, 2008 7: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
Reply    Reply Using Power Editor
  Rank Winnings Points
November 32 $0.00 6
October 0 $0.00 0

Thanks, I got it.
Eswaran Radhakrishnan replied to Raj Cool... on Monday, September 29, 2008 11:04 AM

Thanks
I got it as I want.

Thanks
RE
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

great
Raj Cool... replied to Eswaran Radhakrishnan on Monday, September 29, 2008 11:16 AM

enjoy..
Reply    Reply Using Power Editor
  Rank Winnings Points
November 32 $0.00 6
October 0 $0.00 0