Javascript Regular Expression for International Phone Number - javascript

The following regular expression isn't working for international phone numbers that can allow up to 15 digits:
^[a-zA-Z0-9-().\s]{10,15}$
What needs to be adjusted?

You may find the following regex more useful, it basically first strips all valid special characters which an international phone number can contain (spaces, parens, +, -, ., ext) and then counts if there are at least 7 digits (minimum length for a valid local number).
function isValidPhonenumber(value) {
return (/^\d{7,}$/).test(value.replace(/[\s()+\-\.]|ext/gi, ''));
}

Try adding a backslash:
var unrealisticPhoneNumberRegex = /^[a-zA-Z0-9\-().\s]{10,15}$/;
Now it's still not very useful because you allow an arbitrary number of punctuation characters too. Really, validating a phone number like this — especially if you want it to really work for all possible international phone numbers — is probably a hopeless task. I suggest you go with what #BalusC suggests.

See A comprehensive regex for phone number validation and Is there a standard for storing normalized phone numbers in a database?

and then counts if there are at least 7 digits (minimum length for a valid local number).
The shortest local numbers anywhere in the world are only two or three digits long.
There are many countries without area codes.
There are several well-known places with a 3 digit country code and 4 digit local numbers.
It may be prudent to drop your limit to 6 or 5; just in case.

Related

Regex for validate string of int range

I am trying to build regex that can validate number or range input.
Allowed values are
Any number between 1 and 1816
A range consisting of 2 numbers separated by -. Each number must be between 1 and 1816. E.g. "1-1816", "3-100", "1815-1816"
Invalid values include
"0"
negative numbers (like "-13")
numbers with leading zeros (like "01")
numbers out of range 1-1816 (alone or as part of range)
Any regex will be fine JavaScript or C#.
So far I figured out just
(?<=\s|^)\d+(?=\s|$)
The problem with your regex is that you are accepting all digits via \d and not limiting it to suit your accepted range.
Use this:
^(?:181[0-6]|180\d|1[0-7]\d{2}|[1-9]|\d{2,3})(?:-(?:181[0-6]|180\d|1[0-7]\d{2}|[1-9]|\d{2,3}))?$
This regex limits the numbers to be in the range 1-1816, while supporting either individual numbers or a range via a hyphen separator as specified.
Demo
This regex should do the trick:
^([1-9][0-9]{0,2}|1[0-7][0-9]{2}|180[0-9]|181[0-6])(-([1-9][0-9]{0,2}|1[0-7][0-9]{2}|180[0-9]|181[0-6]))?$
Basically it allows
A number with leading 1 and 0 to 2 more digits
A number with leading 1, followed by 0-7 and twice any number
A number with leading 180 followed by one more number
A number with leading 181 followed by one number 0-6
This optionally once more repeated after -
But generally regexes are not good to work with numbers (and hard to update whne the number changes). If you have such option I'd rather just validate the number by much simpler regex, like
^[1-9][0-9]{0,3}(-([1-9][0-9]{0,3}))?$
and than programmatically split it by - (if present) and parse the individual segments as integer and validate their numeric value against the range.
That would additionally allow you to check for invalid ranges like 100-1, 1800-1800 etc.

How to validate mobile number for countries in angular?

Here I validate mobile number using below code for universally for all countries using below code using this regex (/^(\+\d{1,3}[- ]?)?\d{10}$/) I doing but it not accept this type numbers ((+91)9876543210 OR 09876543210)
This type of mobile Numbers accept
+91 9876543210 (or all other country code)
(+91) 9876543210
(+91)9876543210
+919876543210
+91-9876543210
(+91)-9876543210
9876543210
09876543210
not accept numbers like
++91 9876543210(double ++),
+91 9876543210(double spaces),
+91 987 65 7 4 688(not more than one space),
+91 808-75-74-678(not more than one -),
+91-846363,
80873(number less than 10 digit),
8087339090456(number greater than 10 digit),
0000000000(all zeros),
+91 0000000(all zeros with country code)
HTML
<mat-form-field>
<input matInput placeholder="Enter contact No." [(ngModel)]="userObj.contactno" name="contactNo" required>
</mat-form-field>
<button mat-button color="primary" (click)="saveNewCustomerDetails()">SAVE</button>
TS
saveNewCustomerDetails(){
if(this.userObj.contactno.match(/^(\+\d{1,3}[- ]?)?\d{10}$/)){
console.log("valid mobile number");
}else{
console.log("Invalid mobile number");
}
}
You need to incorporate literal parenthesis in your regex which can be there optionally and since they have special meaning hence you need to escape them in your regex.
Seeing your last mobile number 09876543210, it seems you optionally want to support zero before a number if there is no country code, so you can have zero in alternation for your country code part.
And last, like you don't want to support a number only having all zeroes, you can have a negative look ahead (?!0+$) in your regex. Your updated regex becomes,
^(?!0+$)(?:\(?\+\d{1,3}\)?[- ]?|0)?\d{10}$
Live Demo
Also, you if you don't want to support numbers having only same digits like 2222222222 or 5555555555 then your negative look ahead needs to be written as (?!(\d)\1+$) and your regex will become,
^(?!(\d)\1+$)(?:\(?\+\d{1,3}\)?[- ]?|0)?\d{10}$
Demo for this updated regex
Another way for validating mobile numbers can be, you replace everything that is not a digit and also get rid of all leading zeroes in your number, then what remains will be pure mobile number which you can further validate. But there may be certain disadvantages there as it may become a bit hard to figure out the country code and if the number is really valid. Because certain numbers may be less than ten digits and may actually be invalid but they may be valid if they belong to a country like Singapore which has I guess 8 digit numbers. And same with countries having eleven digits local number might trouble you.
Your pattern is looking correct don't know why it is rejecting, can you try once with
^(\+[\d]{1,5}|0)?[7-9]\d{9}$
Maybe it will work
You can try this..
^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$
This is work for me.

Regex for sting upto 24 characters limit, but should have atleast 10-16 numbers

i am trying to come up with a regex expression. The user can enter a string from with minimum 10 characters and maximum 24 characters, but the string should contain 10 digits and can have a maximum of 16 digits in it.
can someone help. I tried using (?=[0-9]{10,16}) (?=.{10,24})$
One possible approach:
/^(?=.{10,24}$)\D*(?:\d\D*){10,16}$/
The lookahead expression checks the string length, the one after checks the number of digits.
Here's the demo; I've decreased the limits quite a bit for obvious reasons.
The key difference with your pattern is usage of \D*(?:\d\D*) expression; without it only the direct sequence of digits will be matched.
You could always use string match for /[0-9]/g and calc the length of the returned array.
> '93k03k30dk30483'.match(/[0-9]/g).length
11

Expression regular for check phone numbers at word level

I'm trying to write a RegEx to test if a number is valid and for valid I mean any number that matches country calling codes but also where the format of telephone numbers is standardized by ITU-T in the recommendation E.164. This specifies that the entire number should be 15 digits or shorter, and begin with a country prefix as said here so I did this:
^\+\d{2}|\d{3}([0-9])\d{7}$
But it's not working. In my case (VE numbers can't match the RegEx since this one are validated in another way) this input is valid:
+1420XXXXXXXXXXX // Slovakia - X is a digit and could be more, tough, 5 minimum
001420XXXXXXXXXX // Slovakia - I've changed from + to 00
420XXXXXXXXXXXXX // Slovakia - I've removed the 00 o + but number still being valid
+40XXXXXXXXXXXXX // Romania
Invalid numbers are the one that doesn't match the RegEx and the one started with +58 since they are from VE. So, resuming, a valid number should have:
+XX|+XXX plus 12|11 digits (5 minimum) where XX|XXX is the country code and then since maximum is 15 digits then should be 12 or 11 digits depending on the country format
Can any help me with this? It's a one I called complex
Few strange things going on with your regexp:
\d is shorthand for [0-9] - fine to use both, but I'm wondering why they're mixed
what you are searching with you OR (|) is "something that starts with +XX" i.e. plus and two numbers (^\+\d{2}) OR "something that ends with XXXXXXXXXXX" i.e. 11 numbers (\d{3}([0-9])\d{7}$)
You need to group (with brackets) the OR choices, otherwise it is everything to the left or everything to the right (simplistically)
^\+(\d{2}|\d{3})([0-9])\d{7}$
There is, however, another way of giving the number of occurrences : {m,n} means occurs between m and n times. So you could say ^\+\d{7,15}$ (where 7 is your minimum 5 + the minimum country code of 2).
To really do this, however, you might want to take a look here (https://code.google.com/p/libphonenumber/ 1) where there is a complete validation and formatting for all phone numbers available as javascript.

Regular expression that match decimal places

I have this code to test if a value entered in a INPUT is numeric:
$("#price").val().match(/^\d+$/);
I need some help rewriting this function to allow decimal places either with colon or dot meaning for example 230.00 is allowed and 230,00 is allowed too. Also the decimal places could be four or less. Any?
Regex: $("#price").val().match(/^\d+([,\.]\d{1,4})?$/);
Regex n2 with negative values: $("#price").val().match(/^-?\d+([,\.]\d{1,4})?$/);
If you need help parsing values related to some culture in specific, take a loot at this https://github.com/jquery/globalize. It's a library about globalization and localization.
Hope it helps!
$("#price").val().match(/^\d+((\.|,)\d+)?$/);
the above won't limit the number of decimal places to four, though.
By the way, that's a comma and not a "colon"
To match decimal places up to 4 only, use this
$("#price").val().match(/^\d+((\.|,)\d{1,4})?$/);
should match 1 to four numbers after . or ,

Categories

Resources