regex for phone number validation? - regex

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}|...

Related

regular expression for bulgarian mobile phone numbers

Hello I should think of this regular expression:
The telephone number should begin with 087 OR 088 OR 089 and then it should be followed by7 digits:
This is what I made but it doesn't work correctly: it accepts only numbers which begin with 089
(087)|(088)|(089)[0-9]{7}";
/08[789]\d{7}/
that will match 087xxxxxxx, 088xxxxxxx, 089xxxxxxx numbers.
See it in action
Maybe /08[7-9][0-9]{7}/ is what you're searching for?
Autopsy:
08 - a literal 08
[7-9] - matches the numbers from 7-9 once
[0-9]{7} - matches the numbers from 0-9 repeated exactly 7 times
That said, you might prefer /^08[7-9][0-9]{7}$/ if your string is only the phone number. (^ means "the string MUST start here" and $ means "the string MUST end here").
Actually that will be far better regex for Bulgarian phone numbers:
/(\+)?(359|0)8[789]\d{1}(|-| )\d{3}(|-| )\d{3}/
It checks:
Phones that start with country code(+359) or 0 instead;
if the phone number use delimiters like - or space.
I tried it in https://regex101.com and it did not work against my test set. So I tweaked it a little bit with the below regex pattern:
^([+]?359)|0?(|-| )8[789]\d{1}(|-| )\d{3}(|-| )\d{3}$

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}$/;

Regex for telephone number with or without spaces

How do I create a regex that matches telephones with or without spaces in the number?
I have found:
^\+?\d+$
From another post but how do I modify that to allow 0 or more spaces in the number?
The first thing you need to think is the exact format you want for phone numbers containing spaces. Eg:
+535 233 4444
Is that one OK? It means divided like: 3 3 4. You can adapt the following regex to your needs:
^\+?\d{3}\s?\d{3}\s?\{d}{4}$
Just change the quantifiers ({3}, {4}, etc) to change the group lengths.
This is one example:
/^(?:\s*\d{3})?\s*\d{3}\s*\d{4}\s*$/
There's a lot of ways to match telephone numbers (and a lot of valid telephone formats). Here's a simple regex to match "5555555555", "555 555 5555", "(555) 555-5555", "555-555-5555", or "555.555.5555"
^(?\d{3})?( |-|.)?\d{3}( |-|.)?\d{4}$

Custom RegEx expression for validating different possibilities of phone number entries?

I'm looking for a custom RegEx expression (that works!) to will validate common phone number with area code entries (no country code) such as:
111-111-1111
(111) 111-1111
(111)111-1111
111 111 1111
111.111.1111
1111111111
And combinations of these / anything else I may have forgotton.
Also, is it possible to have the RegEx expression itself reformat the entry? So take the 1111111111 and put it in 111-111-1111 format. The regex will most likely be entered in a Joomla / some type of CMS module, so I can't really add code to it aside from the expression itself.
\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})
will match all your examples; after a match, backreference 1 will contain the area code, backreference 2 and 3 will contain the phone number.
I hope you don't need to handle international phone numbers, too.
If the phone number is in a string by itself, you could also use
^\s*\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})\s*$
allowing for leading/trailing whitespace and nothing else.
Why not just remove spaces, parenthesis, dashes, and periods, then check that it is a number of 10 digits?
Depending on the language in question, you might be better off using a replace-like statement to replace non-numeric characters: ()-/. with nothing, and then just check if what is left is a 10-digit number.

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