From what I can see, this should work. The prefix is optional and is stored into the first match group, with the main number going into the second group.
^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$
But if you can give us some test cases for each, it'd help us in giving you a working regex for what you want.
Props to SchlaWiener in the comments for the correct limit on the country code length.
Answer from Joe on Stack OverflowFrom what I can see, this should work. The prefix is optional and is stored into the first match group, with the main number going into the second group.
^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$
But if you can give us some test cases for each, it'd help us in giving you a working regex for what you want.
Props to SchlaWiener in the comments for the correct limit on the country code length.
Simple regexp for Indian mobile number validation: /^[0]?[6789]\d{9}$/
Support 08888888888(Zero appending) 7878787878,8634456876,9545559877(any mobile number precede by 7,8,9 and followed by other 9 digits)
Full function
function mobileNumber(){
var Number = document.getElementById('YOUR ELEMENT ID').value;
var IndNum = /^[0]?[789]\d{9}$/;
if(IndNum.test(Number)){
return;
}
else{
$('#errMessage').text('please enter valid mobile number');
document.getElementById('profile_telephoneNumber').focus();
}
}
Javascript Regex - What to use to validate a phone number? - Stack Overflow
regex - Regular Expression to reformat a US phone number in Javascript - Stack Overflow
validation - RegEx for valid international mobile phone number - Stack Overflow
javascript - How to format mobile phone number with country code using regex - Stack Overflow
Videos
Try this code
HTML Code
<input type="text" id="phone"/>
JS Code
$("#phone").blur(function() {
var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/
var no = $("#phone").val();
if (!regexp.test(no) && no.length < 0) {
alert("Wrong phone no");
}
});
See A comprehensive regex for phone number validation
Quick cheat sheet
- Start the expression:
/^ - If you want to require a space, use:
[\s]or\s - If you want to require parenthesis, use:
[(]and[)]. Using\(and\)is ugly and can make things confusing. - If you want anything to be optional, put a
?after it - If you want a hyphen, just type
-or[-]. If you do not put it first or last in a series of other characters, though, you may need to escape it:\- - If you want to accept different choices in a slot, put brackets around the options:
[-.\s]will require a hyphen, period, or space. A question mark after the last bracket will make all of those optional for that slot. \d{3}: Requires a 3-digit number: 000-999. Shorthand for[0-9][0-9][0-9].[2-9]: Requires a digit 2-9 for that slot.(\+|1\s)?: Accept a "plus" or a 1 and a space (pipe character,|, is "or"), and make it optional. The "plus" sign must be escaped.- If you want specific numbers to match a slot, enter them:
[246]will require a 2, 4, or 6.[77|78]will require 77 or 78. $/: End the expression
Assuming you want the format "(123) 456-7890":
function formatPhoneNumber(phoneNumberString) {
var cleaned = ('' + phoneNumberString).replace(/\D/g, '');
var match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
if (match) {
return '(' + match[1] + ') ' + match[2] + '-' + match[3];
}
return null;
}
Here's a version that allows the optional +1 international code:
function formatPhoneNumber(phoneNumberString) {
var cleaned = ('' + phoneNumberString).replace(/\D/g, '');
var match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/);
if (match) {
var intlCode = (match[1] ? '+1 ' : '');
return [intlCode, '(', match[2], ') ', match[3], '-', match[4]].join('');
}
return null;
}
formatPhoneNumber('+12345678900') // => "+1 (234) 567-8900"
formatPhoneNumber('2345678900') // => "(234) 567-8900"
var x = '301.474.4062';
x = x.replace(/\D+/g, '')
.replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
alert(x);
After stripping all characters except '+' and digits from your input, this should do it:
^\+[1-9]{1}[0-9]{3,14}$
If you want to be more exact with the country codes see this question on List of phone number country codes
However, I would try to be not too strict with my validation. Users get very frustrated if they are told their valid numbers are not acceptable.
Even if you write a regular expression that matches exactly the subset "valid phone numbers" out of strings, there is no way to guarantee (by way of a regular expression) that they are valid mobile phone numbers. In several countries, mobile phone numbers are indistinguishable from landline phone numbers without at least a number plan lookup, and in some cases, even that won't help. For example, in Sweden, lots of people have "ported" their regular, landline-like phone number to their mobile phone. It's still the same number as they had before, but now it goes to a mobile phone instead of a landline.
Since valid phone numbers consist only of digits, I doubt that rolling your own would risk missing some obscure case of phone number at least. If you want to have better certainty, write a generator that takes a list of all valid country codes, and requires one of them at the beginning of the phone number to be matched by the generated regular expression.
Use
^\+[0-9]{1,3} ?[0-9]{10}$
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\+ '+'
--------------------------------------------------------------------------------
[0-9]{1,3} any character of: '0' to '9' (between 1
and 3 times (matching the most amount
possible))
--------------------------------------------------------------------------------
? ' ' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[0-9]{10} any character of: '0' to '9' (10 times)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
PHP:
preg_match('/^\+[0-9]{1,3} ?[0-9]{10}$/', $string)
JavaScript:
/^\+[0-9]{1,3} ?[0-9]{10}$/.test(string)
Unlike our friend. I tried to fix your own pattern:
^\+([0-9][0-9]?[0-9]?)(\ )?([0-9]{10})$
Note that i've removed the ^ from end of pattern because it represents start of a new line!
This guy is your friend: https://regex101.com/
Good luck