0

I want mobile number in format +91(or any other country code)-9999999999(10 digit mobile number).

I have tried /^\+[0-9]{2,3}+[0-9]\d{10}, but its not working please help

Thanks in advance

6 Answers 6

8

Solution in short:

// should return an array with a single match representing the phone number in arr[0]
var arr = '+49-1234567890'.match(/^\+\d{1,3}-\d{9,10}$/); 
// should return null
var nullVal = 'invalid entry'.match(/^\+\d{1,3}-\d{9,10}$/); 

Longer explanation:

  • / start regex
  • ^ try to match from the beginning
  • \+ match a + sign
  • \d{1,3} match a digit 1 to 3 times
  • - match a dash
  • \d{9,10} match 9 or 10 digits
  • $ force the matching to be only valid if can be applied until string termination
  • / finish regex

Knowing what the regex does, might let you modify it to your own needs

Sometimes it is good to ignore any whitespaces you come across. \s* matches 0 or n whitespaces. So in order to be more permissive you could let users input something like ' + 49 - 1232345 '

The regex to match this would be /^\s*\+\s*\d{1,3}\s*-\s*\d{9, 10}\s*$/ (just filled the possible space locations with \s*)

Other than that: I warmly recommend mastering regexes, because they come really handy in many situations.

Sign up to request clarification or add additional context in comments.

2 Comments

Just try the provided regex out in the console of a browser: '+49-1234567890'.match(/^\+\d{1,3}-\d{9,10}$/) (Firefox: Ctrl+shift+K)
It will match only strictly 3 numbers as prefix and 10 numbers as whole number. But won't accept +33-1234567890 (2nrs for prefix) or +123-123456789 (9 nrs for number). To make it flexible you should do /^\+[0-9]{1,3}\-[0-9]{9,10}$/ and since [0-9] is the same as \d aka. a digit you arrive to the formula provided by me
2

If you are expecting a dash in the number (which your format shows), there is nothing in your regex to match it: is the second plus in the regex meant to be a dash?

^\+[0-9]{2,3}-[0-9]\d{10}

Also note that:

  1. some country codes are single digit (eg. 1 for North America, 7 for Russia), these will not be matched
  2. I doubt the local part of all mobiles everywhere is always 10 digits (eg. it won't be enough in countries with large populations as mobile ownership grows)

4 Comments

In France, mobile numbers are 9 digits.
I didnt get u..i want simple country code(1 to 3 digit)-(10 or 9 digit number)
Its nt taking any regex except[+91]..m nt gtng wts the prb
@anupnavare 1. Please write English: heavy use of Text Speak like abreviations will make it much harder for those that don't speak text speak or English as a second language (there is plenty of space for text in comments). 2. You first range will need to be {1,3} to allow one to three digits.
2

You can simply write the following:

var pattern=/^(0|[+91]{3})?[7-9][0-9]{9}$/;

Comments

1
\+[0-9]{2,3}-[0-9]+

Try this. This matches a + in the beginning, two or three numbers for the country code, followed by a - followed by any number of numbers

1 Comment

you might want to add an extra backslash where the backslash is, so that Javascript would read it as a literal backslash
1

Use the mask function

jQuery(function($){
  $("#phone").mask("999-999-9999",{placeholder:" "});   
});

look here http://office.microsoft.com/en-in/access-help/control-data-entry-formats-with-input-masks-HA010096452.aspx

Comments

1

For mobile validation please try this

<html>
<head>
<title>Mobile number validation using regex</title>
<script type="text/javascript">
    function validate() {
        var mobile = document.getElementById("mobile").value;
        var pattern = /^[7-9][0-9]{9}$/;
        if (pattern.test(mobile)) {
            alert("Your mobile number : "+mobile);
            return true;
        }
        alert("It is not valid mobile number");
        return false;

    }
</script>

</head>
<body>
Enter Mobile No. :
<input type="text" name="mobile" id="mobile" />
<input type="submit" value="Check" onclick="validate();" />
</body>
</html>

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.