hi there,
i have a code to validate a tel number but the thing is i want it to restrict it for 10 digits. currently i can add any number of digits how can i restrict it to 10 digits
the code is below

public bool ValidateTelNo(string TelNo)
        {
            bool value = false;

            string rePhoneNumber = @"[1-9]\d{2}\s?\d{3}\d{4}";
            string PhoneNoFormat = @"\([1-9]{1}\d{2}\)\s?\d{3}\-\d{4}";
            Regex re = new Regex(rePhoneNumber);
            Regex re1 = new Regex(PhoneNoFormat);

            if (re.IsMatch(TelNo) || (re1.IsMatch(TelNo)))
                value = true;
           
            else
                value = false;
            
            return value;
        }

thanks
anisha

For starters, you're not defining boundaries on the string, so you'll get false positives for an embedded match. Here's one from my personal library of regular expressions, it more or less rolls your two patterns up into one (but notice the boundary checks):

@"^\(?(?<NPA>[2-9]\d{2})(\)?)(-|.|\s)?(?<NXX3>[1-9]\d{2})(-|.|\s)?(?<NXX4>\d{4})$"

Edited 5 Years Ago by Narue: n/a

Add another if statement to check the length of 'TelNo':

if (TelNo.Length == 10)
{
    //The rest of your code
}
Comments
Nope!

For starters, you're not defining boundaries on the string, so you'll get false positives for an embedded match. Here's one from my personal library of regular expressions, it more or less rolls your two patterns up into one (but notice the boundary checks):

@"^\(?(?<NPA>[2-9]\d{2})(\)?)(-|.|\s)?(?<NXX3>[1-9]\d{2})(-|.|\s)?(?<NXX4>\d{4})$"

hey please can you explain this more

thanx

If you are using windows forms: every textbox has a property called'MaxLength', set that to '10'.

Adding the if statement In my last reply to your validator will ensure that the validator only returns true if the text entered into the textbox is 10 letters in length.

understand?

If you are using windows forms: every textbox has a property called'MaxLength', set that to '10'.

Adding the if statement In my last reply to your validator will ensure that the validator only returns true if the text entered into the textbox is 10 letters in length.

understand?

i understood your post

C# Syntax (Toggle Plain Text)

   1.
      @"^\(?(?<NPA>[2-9]\d{2})(\)?)(-|.|\s)?(?<NXX3>[1-9]\d{2})(-|.|\s)?(?<NXX4>\d{4})$"

the above can't understand

The regex is creating three named matches (so you can get each part of the phone number) called NPA, NXX3 and NXX4. So we have this: ^ - must match the beginning of the string \(? - starts with a paren (the ? makes it optional) (?<NPA>[2-9]\d{2}) - Create a named match that starts with a digit from 2-9 and has 2 more digits (\)?) - closing paren. Again, optional (-|.|\s)? - followed by a dash, a period or a whitespace (optional)

These are then followed by a 3 digit match, the optional separator and then a 4 digit match. Finally, the $ means match the end of the string

^                         // Match the beginning of a string
    \(?                   // Match a literal '(' (zero or one)
    (?<NPA>               // Create a capture group named NPA
        [2-9]             // Match one digit in the range of [2-9]
        \d{2}             // Match two digits
    )                     // End the NPA capture group
    (\)?)                 // Match a literal ')' (zero or one)
    (-|.|\s)?             // Match zero or one of '-', '.', or a whitespace character
    (?<NXX3>              // Create a capture group named NXX3
        [1-9]             // Match one digit in the range of [1-9]
        \d{2}             // Match two digits
    )                     // End the NXX3 capture group
    (-|.|\s)?             // Match zero or one of '-', '.', or a whitespace character
    (?<NXX4>              // Create a capture group named NXX4
        \d{4}             // Match four digits
    )                     // End the NXX4 capture group
$                         // Match the end of a string

NPA (Number Plan Areas) is the area code. Since surrounding parens are optional, they're not included in the capture group (which you would use to extract them if doing more than just an existence match). The NXX (Central Office Exchange Code) groups represent the three digit and four digit parts of the local number. Like NPA, you can extract them independently due to the grouping.

Comments
:)
This article has been dead for over six months. Start a new discussion instead.