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

Asked By Eswaran Radhakrishnan
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  I have tested this code

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  output of program

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  re

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  re
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  use this
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.?  working fine. How to restrict numbers in regex.?
29-Sep-08 05:32 AM
end of post
Working fine. How to restrict Number in regex?  Working fine. How to restrict Number in regex?
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  solution
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.  Thanks, I got it.
29-Sep-08 11:04 AM
Thanks
I got it as I want.

Thanks
RE
great  great
29-Sep-08 11:16 AM
enjoy..
Create New Account
help
CREATE TABLE [T_SPACEUSEAGREEMENT] ( [spec_id] [numeric](20, 0) NOT NULL , [Sys_projectid] [numeric](20, 0) NULL , [Sys_objectstate] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [Sys_objectId] [numeric](20, 0) NULL , [Sys_guiid] [numeric](20, 0) NULL , [Sys_ReviewStatus] [varchar] (1000) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [Sys_PriorState] [varchar] (1000) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_Security] [numeric](32, 12) NULL , [ce_Security_UOM varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_PropertyTaxes] [numeric](32, 12) NULL , [ce_PropertyTaxes_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_Insurance] [numeric](32, 12) NULL , [ce_Insurance_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_Electricity] [numeric](32, 12) NULL , [ce_Electricity_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_UtilitiesBase] [numeric](32, 12) NULL , [ce_UtilitiesBase_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_SecurityBase] [numeric](32, 12) NULL , [ce_SecurityBase_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL
How can we know a state of a thread? (A) What is use of Interlocked class ? (A) What is a monitor object? (A) What are wait handles? (A) What is ManualResetEvent and Webservices (B)What is an application domain? (B) What is .NET Remoting? (B) Which class does the remote object has to inherit? (I) what are two different types of remote Cache substitution? Chapter 6: OOPS (B) What is Object Oriented Programming? (B) What is a Class? (B) What is an Object? (A) What is the relation between Classes and Objects? (B Overriding? (I) what is the difference between delegate and events? (B) If we inherit a class do the private variables also get inherited? (B) What is the different accessibility levels defined in .NET? (I) Can you prevent a class from overriding? (I) what is the use of “Must inherit” keyword in VB.NET? (I Do interface have accessibility modifier. (A) What are similarities between Class and structure? (A) What is the difference between Class and structure’s? (B) What does virtual keyword mean? (B) What are shared (VB.NET 9: ADO.NET (B) What is the namespace in which .NET has the data functionality class? (B) Can you give an overview of ADO.NET architecture? (B) What are the two
Interface and Abstract Class Hai, When do we go for Interface. . When do we go for Abstract Class. . .Give one example Today i faced one interview . . He asked this question.I want answer with Example Thanks in Advance Abstract Class :: - It cannot defines all the methods - It has subclass. - Here, Subclass is useless - A class can be extend an abstract class Interface :: - It defines all the methods - It must have implementations by other classes, But there http: / / kyapoocha.com / c-sharp-interview-questions / what%E2%80%99s-the-difference-between-an-interface-and-abstract-class-5 / http: / / www.dotnetuncle.com / Difference / 4_abstract_class_interface.aspx Hope this helps. HI An interface contains signatures of methods , delegates or events . The implementation of the methods is done in the class that implements the interface, as shown in the following example: interface ISampleInterface { void SampleMethod(); } class ImplementationClass : ISampleInterface { / / Explicit interface member implementation: void ISampleInterface.SampleMethod() { / / Method implementation. } static void Main() { / / Declare
attribute like Summary, Keyword, Title, Author. . .etc. . . . without storing on the server. . . . Thanks, Arth <pre class = "csharpcode" > &lt;pre <span class = "kwrd" > class < / span> = <span class = "str" > "csharpcode" < / span> &gt;&amp;lt;pre &lt;span <span class = "kwrd" > class < / span> = <span class = "str" > "kwrd" < / span> &gt;<span class = "kwrd" > class < / span> &lt; / span&gt; = &lt;span <span class = "kwrd" > class < / span> = <span class