Specific Regex Expression for Phone Numbers - regex

I need to validate that phone numbers are entered in this specific format
+[country_code][space][phone_number], where
+ is required
<country_code> is required, 0-9 only, at least 1 digit, max 6 digits
<space> is required
<phone_number> is required, 0-9 only, at least 4 digits, max 20 digits
Match: +1 2123333455, +98 343545454, +8733 343433434
Non-Match: +1 (212) 444-3388, 98 3434343, +334 34343 434343
Thanks a bunch! I am not a regex expert.

^\+\d{1,6} \d{4,20}$
should do.
Here are some tests.
If you don't want the country code to begin with a 0 :
^\+[1-9]\d{0,5} \d{4,20}$

This will validate any international number
/\^+[0-9]{1,3}\.[0-9]{4,20}$/
The number should start with a plus sign followed by the country code and national number

Related

Regex for UK phone number

I need to validate uk numbers
Below are sample type of number
01457 341235
0229 111111
+1213 3133143
Optional Plus should be allowed at first postion only
Using this regex but not working
^(?:\W*\d){11}\W*$
An actual UK phone number will start with 0 or +44 (the latter being the UK country code), or possibly just 44, followed by nine or ten digits. A regex to capture that would look something like:
^(?:0|\+?44)(?:\d\s?){9,10}$
In this regex, I have allowed the digits to be separated by spaces in any way, because there isn't a single standardized way of breaking down the numbers. You could further narrow this down to certain allowed groupings, if you like, but it would greatly increase the complexity of the regex.
Your question implies you might want something broader or different. As some of your examples aren't valid UK numbers (+1213 3133143, 12345 123456).
You could use something like this to simply match between 10 and 12 digits, with arbitrary spacing, possibly preceded by a +:
^\+?(?:\d\s?){10,12}$

Regular Expression Validation For Indian Phone Number and Mobile number

I want to validate Indian phone numbers as well as mobile numbers. The format of the phone number and mobile number is as follows:
For land Line number
03595-259506
03592 245902
03598245785
For mobile number
9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389
I would like the regular expression in one regex. I have tried the following regex but it is not working properly.
{^\+?[0-9-]+$}
For land Line Number
03595-259506
03592 245902
03598245785
you can use this
\d{5}([- ]*)\d{6}
NEW for all ;)
OLD: ((\+*)(0*|(0 )*|(0-)*|(91 )*)(\d{12}+|\d{10}+))|\d{5}([- ]*)\d{6}
NEW: ((\+*)((0[ -]*)*|((91 )*))((\d{12})+|(\d{10})+))|\d{5}([- ]*)\d{6}
9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389
03595-259506
03592 245902
03598245785
this site is useful for me, and maby for you .;)http://gskinner.com/RegExr/
Use the following regex
^(\+91[\-\s]?)?[0]?(91)?[789]\d{9}$
This will support the following formats:
8880344456
+918880344456
+91 8880344456
+91-8880344456
08880344456
918880344456
This works really fine:
\+?\d[\d -]{8,12}\d
Matches:
03598245785
9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389
987-98723-9898
+91 98780 98802
06421223054
9934-05-4851
WAQU9876567892
ABCD9876541212
98723-98765
Does NOT match:
2343
234-8700
1 234 765
for mobile number:
const re = /^[6-9]{1}[0-9]{9}$/;
I use the following for one of my python project
Regex
(\+91)?(-)?\s*?(91)?\s*?(\d{3})-?\s*?(\d{3})-?\s*?(\d{4})
Python usage
re.search(re.compile(r'(\+91)?(-)?\s*?(91)?\s*?(\d{3})-?\s*?(\d{3})-?\s*?(\d{4})'), text_to_search).group()
Explanation
(\+91)? // optionally match '+91'
(91)? // optionally match '91'
-? // optionally match '-'
\s*? // optionally match whitespace
(\d{3}) // compulsory match 3 digits
(\d{4}) // compulsory match 4 digits
Tested & works for
9992223333
+91 9992223333
91 9992223333
91999 222 3333
+91999 222 3333
+91 999-222-3333
+91 999 222 3333
91 999 222 3333
999 222 3333
+919992223333
For both mobile & fixed numbers: (?:\s+|)((0|(?:(\+|)91))(?:\s|-)*(?:(?:\d(?:\s|-)*\d{9})|(?:\d{2}(?:\s|-)*\d{8})|(?:\d{3}(?:\s|-)*\d{7}))|\d{10})(?:\s+|)
Explaination:
(?:\s+|) // leading spaces
((0|(?:(\+|)91)) // prefixed 0, 91 or +91
(?:\s|-)* // connecting space or dash (-)
(?:(?:\d(?:\s|-)*\d{9})| // 1 digit STD code & number with connecting space or dash
(?:\d{2}(?:\s|-)*\d{8})| // 2 digit STD code & number with connecting space or dash
(?:\d{3}(?:\s|-)*\d{7})| // 3 digit STD code & number with connecting space or dash
\d{10}) // plain 10 digit number
(?:\s+|) // trailing spaces
I've tested it on following text
9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389
0359-2595065
0352 2459025
03598245785
07912345678
01123456789
sdasdcsd
+919898101353
dasvsd0
+91 dacsdvsad
davsdvasd
0112776654
You can use regular expression like this.
/^[(]+\ ++\d{2}[)]+[^0]+\d{9}/
For Indian Mobile Numbers
Regular Expression to validate 11 or 12 (starting with 0 or 91) digit number
String regx = "(0/91)?[7-9][0-9]{9}";
String mobileNumber = "09756432848";
check
if(mobileNumber.matches(regx)){
"VALID MOBILE NUMBER"
}else{
"INVALID MOBILE NUMBER"
}
You can check for 10 digit mobile number by removing "(0/91)?" from the regular expression i.e. regx
you can implement following regex
regex = '^[6-9][0-9]{9}$'
All mobile numbers in India start with 9, 8, 7 or 6. Now, there is a chance that you are not bothering about the prefixes (+91 or 0). If this is your scenario, then you can take the help from the website regextester.com or you can use r'^(+91[-\s]?)?[0]?(91)?[789]\d{9}$'
And if you want to validate the Phone number with prefixes(+91 or 0) then use : r'^[6-9]\d{9}$'.
r'\+?(91?|0?)[\-\s]?[3-9]\d{3}[\-\s]?\d{6}$'
explanation
+? # Start with plus sign or not
(91?|0?) # Followed by 91 or 0 or none of them
[-\s]? # Followed by either - or space, or none of them
[3-9] # followed by any number from 3 between 9
\d{3} # followed by any three digits
\d{6} # followed by any six digits
$ # specify string should stop at that point
You Can Use Regex Like This:
^[0-9\-\(\)\, ]+$
All Landline Numbers and Mobile Number
^[\d]{2,4}[- ]?[\d]{3}[- ]?[\d]{3,5}|([0])?(\+\d{1,2}[- ]?)?[789]{1}\d{9}$
var phonereg = /^(\+\d{1,3}[- ]?)?\d{10}$/;

Regular expression for price validation

Need regular expression which have:
Maximum 8 digits before decimal(.) point
Maximum 4 digits after decimal point
Decimal point is optional
Maximum valid decimal is 8 digits before decimal and 4 digits after decimal
So 99999999.9999
The regular rexpression I have tried ^\d{0,8}[.]?\d{1,4}$ is failing for 123456789
and more than this. means it is taking more than 8 digits if decimal point is not available.
Tested here : http://regexpal.com/
Many many thanks in advance!
^\d{0,8}(\.\d{1,4})?$
You can make the entire decimal optional
You can try this:
^\d{1,8}(?:\.\d{1,4})?$
or
^[1-9]\d{0,7}(?:\.\d{1,4})?$
If you don't want to have a zero as first digit.
You can allow this if you want: (.1234)
^[1-9]\d{0,7}(?:\.\d{1,4})?|\.\d{1,4}$
Any of the above did not work for me.
Only this works for me
^([0-9]{0,2}((.)[0-9]{0,2}))$
This regex is working for most cases even negative prices,
(\-?\d+\.?\d{0,2})
Tested with the following,
9
9.97
37.97
132.97
-125.55
12.2
1000.00
10000.00
100000.00
1000000.00
401395011
If there is a price of $9.97, £9.97 or €9.97 it will validate 9.97 removing the symbol.
1-(\$+.[1-9])
2-(\£+.[1-9])
You can use this expression for complete price digits.
I'm using this:
^[1-9]\d{0,7}(\.\d{1-4})$
^ = the start of the string
[1-9] = at least the string has to begin with 1 number between 1 and 9
\d{0,7} = optional or max 7 times d (digit: a number between 0 and 9)
() = create a group like a substring
. = need a .
\d{1-4} = digit repited max 4 time
$ end of the string
For price validation we can not allow inputs with leading repeating zeros like 0012 etc.
My solution check for any cases. Also it allows maximum 2 decimal point after the dot.
^(?:0\.[0-9]{1,2}|[1-9]{1}[0-9]*(\.[0-9]{1,2})?|0)$

regex for phone number validation?

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?
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})/
Straightforward solution is simple, use |
String ex = "\\d{3}-\\d{8} / \\d{4}|\\(\\d{3}\\) \\d{8}|...

Validation for a 10 digit phone number

I'm looking for a simple regex that will validate a 10 digit phone number. I'd like to make sure that the number is exactly 10 digits, no letters, hyphens or parens and that the first two digits do not start with 0 or 1. Can someone help out?
/[2-9]{2}\d{8}/
^[2-9]{2}[0-9]{8}$
I consider [0-9] to be better to read than \d, especially considering the preceding [2-9]
The ^ and $ ensure that the input string consists ONLY of those 8 characters - otherwise it is not guaranteed that the input string is not larger - i.e. "12345678901" would match the regex w/o those two characters - although it is 11 chars and starts with a 1!
As Randal pointed out, this question is not consistent with the way phone numbers are formatted in North America (even though the OP stated 'first two digits do not start with 0 or 1'). A better regex for North American phone numbers would be:
^[2-9]{1}[0-9]{9}$
For example, Washington DC's area code is (202). NYC has area code (212). Northern New Jersey has (201).
But more accurately, the NANP has a lot of rules as it relates to what is allowed in area code and exchange (first six digits). This regex should still cover most cases. https://en.wikipedia.org/wiki/North_American_Numbering_Plan
This regex script might help out. I essentially strips any "punctuation" characters, including a leading 1-, then validates it is 10 digits.
The extra part you probably don't need is the formatting to 000-000-0000
formatPhone = function() {
var phone = this.value;
phone = phone.replace(/^1(|-|\(|\)|\.| )*|-|\(|\)|\.| /g, '');
if(phone.length === 10) {
this.value = phone.slice(0,3) + '-' + phone.slice(3,6) + '-' + phone.slice(6,10);
}
}
The Phone Numbers will be of 10 digits, and it will start from 7,8 and 9
[RegularExpression("^([07][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 8[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] | 9[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])$", ErrorMessage = "Enter Valid Mobile Number")]
reference : http://www.regular-expressions.info/numericranges.html