I use a regular expression to validate a Dutch mobile phone number:
^((\+316|06|00316){1}\\s?-?\s?[1-9]{1}[0-9]{7})$
So the number should start with 06 or +316 or 00316 followed by 8 digits (not starting with a 0)
Now I have to extend the validation so it also allows for international numbers from outside The Netherlands.
I want to keep it simple so I was thinking:
If it not starts with 06 or +316 or 00316 then it should start with '00dd' or '+dd' where dd!=31 followed by 8 till 12 digits.
so 0032127384562 should match but 00317198765432 not (because it starts with 0031 but not 00316)
Does anybody know how to do this?
The regex for the international number is:
^00(?!31)[0-9]{2}\s?-?\s?[1-9][0-9]{7,11}$
or, if you can't use lookahead:
^00([0-24-9][0-9]|3[02-9])\s?-?\s?[1-9][0-9]{7,11}$
You can concatenate it with | and the Dutch regex.
Be aware that there is a typo in your regex at \\.
Related
Every time this is asked, the answer is to use negative lookahead like so: (?!x)
Well unfortunately I must implement an American Express check in vb6, which does NOT support negative lookaheads or negative look behinds.
Is it possible to match everything except 34, 37 using the oldest imaginable implementation of regular expressions?
I expected this sort of thing to work:
^[1,2,4-9][1-3,5-6,8-9][0-9]{13}$
I've been able to match AmEx with this expression of course: ^3[47][0-9]{13}$ but I haven't found a way to inverse the validation rule.
Here is my validator setup:
<asp:RegularExpressionValidator
ID="AmExValidator"
Display="None"
ControlToValidate="txtCreditCardNumber"
ValidationExpression="^[1,2,4-9][1-3,5-6,8-9][0-9]{13}$"
ErrorMessage="American Express cards are not accepted."
ValidationGroup="Payment"
runat="server">
</asp:RegularExpressionValidator>```
You can use
^([0-24-9][0-9]|3[0-35689])[0-9]{13}$
See the regex demo. Details:
^ - start of string
([0-24-9][0-9]|3[0-35689]) - 00 to 29 and 40 to 99, or 3 followed with a digit other than 4 and 7
[0-9]{13} - thirteen digits
$ - end of string.
I have the following 2 regex for UK mobile phone number and for UK landline numbers respectively-
^0(7\\d{9})\$
^0([1-6]\\d{8,10})\
I need to combine them into one regex to validate an input field using JavaScript to be any UK mobile or landline. I’ve tried using the | (pipe symbol) for ‘or’, but that’s not working and doesn’t recognise either of them.
Anyone any suggestions as to how best to combine.
Many thanks in advance,
If you want to combine the patterns, you may use
^0([1-6][0-9]{8,10}|7[0-9]{9})$
Note that the common prefix is outside the parentheses, the rest is an alternation group with anchors outside of the group.
Details
^ - start of string
0 - a zero
([1-6][0-9]{8,10}|7[0-9]{9}) - either of the two alternatives
[1-6][0-9]{8,10} - a digit from 1 to 6 and then 8 to 10 digits
| - or
7[0-9]{9} - 7 and any 9 digits
$ - end of string.
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}$
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}|...
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.