0

i want to validate my phone number with the regex for following formats.i have googled the things but i could not find the regex for following formats...

    079-26408300 / 8200
    (079) 26408300
    079 264 083 00 
    9429527462

can anyone please guide me how can i do validate the phone number field for above formats? I want to validate the phone number for only above formats as right now am using only following regex var phone_pattern = /^[a-z0-9]+$/i;

@Ali Shah Ahmed

var phone_pattern = "(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})";

here is the way am checking if its valid

if (!phone_pattern.test(personal_phone))
        {
            $("#restErrorpersonalphone").html('Please enter valid phone number');
            $("#personal_phone").addClass('borderColor');
            flag = false;
        } else {
            $("#restErrorpersonalphone").html('');
            $("#personal_phone").removeClass('borderColor');
        }

its not working. Am I implementing in wrong way?

4
  • 3
    Wouldn't it be better to just strip away everything but numbers and deal with the raw numbers, no spaces, parentheses, pluses, etc? Commented Jan 30, 2013 at 4:55
  • ok..let say i dont want parenthesis +.but its required to use - ..please see my updated question Commented Jan 30, 2013 at 4:59
  • Have you search SO for this before posting? There are many post about validating a phone number. Commented Jan 30, 2013 at 5:24
  • Yes I searched but couldnt find the way i want Commented Jan 30, 2013 at 5:40

2 Answers 2

1

lets start with the simplest phone number 9429527462
As this has 10 characters and all are numbers, regex for it could be \d{10}

Now the next phone number 079 264 083 00. Regex for this pattern could be (\d{3}\s){3}\d{2} First we are expecting a group of 3 digits and a space to repeat thrice (\d{3}\s){3}, this will cover 079 264 083 (space included in it), so left will be the last two characters which are handled using \d{2}

For the phone number (079) 26408300, \(\d{3}\)\s\d{8} regex could be use. The regex first looks for a opening bracket, then three digits inside it, and then the closing bracket. It then looks for a space, and then for 8 digits.

The phone number 079-26408300 / 8200 could be validated using regex \d{3}-\d{8}\s/\s\d{4}. It first looks for 3 digits then a -, then 8 digits followed by a space. Then looks for a / and then a space and then 4 digits.

If you wish to know a single regex for validating all the above patterns, do let me know.

Final combined regex would be:

/(\d{10})|(\d{3}-\d{8}\s\/\s\d{4})|((\d{3}\s){3}\d{2})|(\(\d{3}\)\s\d{8})/
Sign up to request clarification or add additional context in comments.

8 Comments

yes ..if u can provide me single regex then it will be useful..thanks for this wonderful answer with details
well i would keep it as simple as possible. "(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})" (Please note there is no need of quotation marks, i put these just to seperate the regex from rest of the comment) I believe this regex would do the work. What i did is just combined all the mentioned regex with OR statements. So it would match all the mentioned patterns.
can u please see my updated question am quite new in this regex so..please help me as i dont know how i put this in my javascript
there are some small mistakes. Firstly do not put your regex in inverted commas. var phone_pattern = /(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})/ start and end your regex with a forward slash. Secondly, the regex contains a forward slash in itself, try replacing that forward slash with a backslash and a forward slash, "\/" So finally the code will look like: var phone_pattern = /(\d{10})|(\d{3}-\d{8}\s\/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})/
hey i just noticed that two of the backslashes are removed in the comment, so i updated my answer with the final regex. Try following the steps mentioned in my previous comment, and replace the final regex with the one mentioned in my answer.
|
0

Straightforward solution is simple, use |

String ex = "\\d{3}-\\d{8} / \\d{4}|\\(\\d{3}\\) \\d{8}|...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.