How to validate mobile number for countries in angular? - javascript

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.

Related

Multiple type Phone Number format javascript in PDF

I'm using NitroPDF to make custom forms and I have a problem....
Although NitroPDF allow me to format numbers using Arbitraty Mask, it only allow to use one custom pattern. But in my country, landline numbers have less numbers than cellphone numbers.
For example:
Landline: (99) 9999-9999
Cellphone: (99) 99999-9999
How could I let NitroPDF identify and apply mask based on field size, using javascript?
I don't know if this Arbitraty Mask allow me to do something like:
(99) 9999-9999 or (99) 99999-9999
Can someone help me? :)
Could you just specify the amount of digits you want with a limiting range?
(\(\d{2}\) \d{4,5}-\d{4})
So this looks for exactly two digits inside of parenthesis, followed by a space. Then it checks for either four or five digits. Finally, it looks for a dash followed by exactly four more digits.
Here is a demo

Can this numeric range regex be refactored?

I need to match a number range:
-9223372036854775808 to 9223372036854775807
^(?:922337203685477580[0-7]|9223372036854775[0-7]\d{2}|922337203685477[0-4]\d{3}|92233720368547[0-6]\d{4}|9223372036854[0-6]\d{5}|922337203685[0-3]\d{6}|92233720368[0-4]\d{7}|9223372036[0-7]\d{8}|922337203[0-5]\d{9}|92233720[0-2]\d{10}|922337[0-1]\d{12}|92233[0-6]\d{13}|9223[0-2]\d{14}|922[0-2]\d{15}|92[0-1]\d{16}|9[01]\d{17}|[1-8]\d{18}|\d{0,18}|-(?:922337203685477580[0-8]|9223372036854775[0-7]\d{2}|922337203685477[0-4]\d{3}|92233720368547[0-6]\d{4}|9223372036854[0-6]\d{5}|922337203685[0-3]\d{6}|92233720368[0-4]\d{7}|9223372036[0-7]\d{8}|922337203[0-5]\d{9}|92233720[0-2]\d{10}|922337[0-1]\d{12}|92233[0-6]\d{13}|9223[0-2]\d{14}|922[0-2]\d{15}|92[0-1]\d{16}|9[01]\d{17}|[1-8]\d{18}|\d{0,18}))?$
// space for easier copy and paste
Yes, I know it sounds crazy, but there's a long story behind this. I can't figure out how to do this in JavaScript by just checking a range, because of the size of the number, and this must be accurate.
Here's the thought process in breaking this thing down. I just started with the max number and worked my way down, then worked on the negative by just adding the - in the regex. You'll obviously have to copy and paste this thing somewhere to see it all. Also, could be mistakes. Made my head nearly explode.
9,223,372,036,854,775,807
922337203685477580[0-7]
9223372036854775[0-7][0-9]{2}
922337203685477[0-4][0-9]{3}
92233720368547[0-6][0-9]{4}
9223372036854[0-6][0-9]{5}
922337203685[0-3][0-9]{6}
92233720368[0-4][0-9]{7}
9223372036[0-7][0-9]{8}
922337203[0-5][0-9]{9}
92233720[0-2][0-9]{10}
922337[0-1][0-9]{12}
92233[0-6][0-9]{13}
9223[0-2][0-9]{14}
922[0-2][0-9]{15}
92[0-1][0-9]{16}
9[01][0-9]{17}
[1-8][0-9]{18}
[0-9]{0,18}
There's a single digit different in the negative vs. positive, so you'll see where I had to basically duplicate most of this.
So a few question:
Did I do this right?
If not, what's a better way?
Can this be done without regular expressions considering the size of the number? I need to validate client-side.
Can it be refactored and still retain strict rules?
Suggestions appreciated :)
Can this be done without regular expressions considering the size of the number?
It can be done in a series of if statements using only string operations (no need to convert to numbers).
all strings that don't match [0-9]{1,19} are out
all candidates that are of length 18 or less are good
for length 19 you can work with string comparison to see if they are numerically less than your upper limit
tweak the above to take care of negative numbers
Your regex is correct.
This is a shorter version
^(?:-9223372036854775808|-?(?:\d{0,18}|(?!922337203685477580[8-9]|92233720368547758[1-9]|92233720368547759|922337203685477[6-9]|92233720368547[8-9]|9223372036854[8-9]|922337203685[5-9]|92233720368[6-9]|92233720369|922337203[7-9]|92233720[4-9]|9223372[1-9]|922337[3-9]|92233[8-9]|9223[4-9]|922[4-9]|92[3-9]|9[3-9])\d{19}))$
Regex demo
How to generate that regex without mistake:
Input max number:
9223372036854775807
Output:
9223372036854775807
922337203685477580
92233720368547758
9223372036854775
922337203685477
92233720368547
9223372036854
922337203685
92233720368
9223372036
922337203
92233720
9223372
922337
92233
9223
922
92
9
Replace last number letter
9->remove all line
8->9
7->[8-9]
6->[7-9]
5->[6-9]
4->[5-9]
3->[4-9]
2->[3-9]
1->[2-9]
0->[1-9]
Output:
922337203685477580[8-9]
92233720368547758[1-9]
92233720368547759
922337203685477[6-9]
92233720368547[8-9]
9223372036854[8-9]
922337203685[5-9]
92233720368[6-9]
92233720369
922337203[7-9]
92233720[4-9]
9223372[1-9]
922337[3-9]
92233[8-9]
9223[4-9]
922[4-9]
92[3-9]
9[3-9]
Regex [Output]
922337203685477580[8-9]|92233720368547758[1-9]|92233720368547759|922337203685477[6-9]|92233720368547[8-9]|9223372036854[8-9]|922337203685[5-9]|92233720368[6-9]|92233720369|922337203[7-9]|92233720[4-9]|9223372[1-9]|922337[3-9]|92233[8-9]|9223[4-9]|922[4-9]|92[3-9]|9[3-9]
Add these[output] to regex
(?!output)\d{19}
Will become [output2]
(?!922337203685477580[8-9]|92233720368547758[1-9]|92233720368547759|922337203685477[6-9]|92233720368547[8-9]|9223372036854[8-9]|922337203685[5-9]|92233720368[6-9]|92233720369|922337203[7-9]|92233720[4-9]|9223372[1-9]|922337[3-9]|92233[8-9]|9223[4-9]|922[4-9]|92[3-9]|9[3-9])\d{19}
Matches \d{19} <= 9223372036854775807
Add
^(?:-9223372036854775808|-?(?:\d{0,18}|[output2]))$
^(?:-9223372036854775808|-?(?:\d{0,18}|(?!922337203685477580[8-9]|92233720368547758[1-9]|92233720368547759|922337203685477[6-9]|92233720368547[8-9]|9223372036854[8-9]|922337203685[5-9]|92233720368[6-9]|92233720369|922337203[7-9]|92233720[4-9]|9223372[1-9]|922337[3-9]|92233[8-9]|9223[4-9]|922[4-9]|92[3-9]|9[3-9])\d{19}))$
Will match
-9223372036854775808 or
+/- \d{0,18} or
+/- \d{19} <= 9223372036854775807
Demo

Regex to validate number between two ranges

how to validate numbers between 0 to 99999.99 using regex?
basically it should accept the values like 0.01, 0000.01, .01 and 9999.99
also it should have only two values after dot(.)
I tried something like the below but it did not work for me.
/^[0-9]{1,5}.[0-9]{2}$/
decimal is optional
Could someone help pls.
The . character has special meaning in a regex, so you need to escape it.
Anyway, let me see if I got the rules straight:
Decimal point is optional
If decimal point not given:
Between 1 and 5 digits
If decimal point is present:
Between 0 and 5 digits before decimal point
Between 1 and 2 digits after decimal point
Since the number of digits depends on the presence of the decimal point, you make the regex have two choices, separated by |.
Choice 1 (no decimal point): [0-9]{1,5}
Choice 2 (decimal point): [0-9]{0,5}\.[0-9]{1,2}
Since you want anchors (^$), you can either put them in both choices, or surround the choice set with parenthesis. To make it non-capturing, use (?:xxx).
Final regex is one of these:
/^[0-9]{1,5}$|^[0-9]{0,5}\.[0-9]{1,2}$/
/^(?:[0-9]{1,5}|[0-9]{0,5}\.[0-9]{1,2})$/
You can see the second one in effect on regex101.
If javascript then below regex would work:
^\d{0,5}(((\.){0})|((\.){1}\d{1,2}))$
All the above mentioned cases are satisfying.
Thank you and ^(?:\d{1,5}(?:\.\d{1,2})?|\.\d{1,2})$ is what i was trying to get and this serves my purpose.
Javascript regex:
/^\d{1,5}(\.\d{1,2})?$/
Javascript demo

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.

Javascript Regular Expression for International Phone Number

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.

Categories

Resources