hi there,
i may have asked this question several times, but the tel number is being formated in a incorrect way.

when the user enters the tel number i run the below code

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

            string rePhoneNumber = @"\d{3}\s?\d{3}\d{4}$";
                string PhoneNoFormat = @"\(\d{3}\)\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;
        }

and then i run the second code which is shown below.
for example if the user enters 12345678901
the tel number will be formatted as (1234) 567-8901
how can i fix this so the it will return a error message saying invalid tel number the format should be (xxx) xxx-xxxx

please can some one guide me

thanks

public string CheckingValue(string Value)
        {
            long value = 0;

            string str = Value;
            bool bChecking = long.TryParse(str, out value);
            if (bChecking)
            {
                str = String.Format("{0:(###) ###-####}", value);
            }

            return str;
        }

Recommended Answers

All 6 Replies

public string CheckingValue(string Value) {
    long value = 0;

    string str = null;
    if (Value.Length == 10 && long.TryParse(Value, out value)) {
        str = String.Format("{0:(###) ###-####}", value);
    }

    return str;
}

Check the return value for null and display an error message if it is.

public string CheckingValue(string Value) {
    long value = 0;

    string str = null;
    if (Value.Length == 10 && long.TryParse(Value, out value)) {
        str = String.Format("{0:(###) ###-####}", value);
    }

    return str;
}

Check the return value for null and display an error message if it is.

how do i do that?

the thing is the tel number will format as (1234) 567-8901

how do i make it as (123) 456-7980

thanks

It won't format as (1234) 567-8901 if you use the code I provided above.

You use it like this:

String telephone = "12345678901234";
String formatedNumber = CheckingValue(telephone);
if (formatedNumber == null) {
    // there was an error do whatever you need here
} else {
    // number is good, do whatever here
}

It won't format as (1234) 567-8901 if you use the code I provided above.

You use it like this:

String telephone = "12345678901234";
String formatedNumber = CheckingValue(telephone);
if (formatedNumber == null) {
    // there was an error do whatever you need here
} else {
    // number is good, do whatever here
}

no no,
i wan't (1234) 567-8901 to say as error tel number and to format as (123) 567-8901
with in brackets there should be only 3 numbers that is what i want

thanks

How do you know which 3 numbers should be used? Given "12345678901" is the number (123) 456-7890 or (234) 567-8901 or (123) 567-8901?

How do you know which 3 numbers should be used? Given "12345678901" is the number (123) 456-7890 or (234) 567-8901 or (123) 567-8901?

"(123) 456-7890" should be the correct one if anything else it should display an invalid tel no

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.