Recently I was tasked with writing a regular expression that would check for a valid Australian phone number for both landline and mobile phone variants whilst allowing for different formats (spaces, no spaces, international dialing code, brackets, area code, no area code).
The below regular expression code and subsequent tests are for Australian mobile and landline numbers only, but could be tweaked to work for other countries as well.
The expression
// This is our bread and butter expression
var phoneExpression = /^\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{2}(\ |-){0,1}[0-9]{1}(\ |-){0,1}[0-9]{3}$/;
// Valid
var phoneNumber1 = "0411 234 567";
var phoneNumber2 = "(02) 3892 1111";
var phoneNumber3 = "38921111";
var phoneNumber4 = "0411234567";
// Invalid
var phoneNumber5 = "3892 11";
var phoneNumber6 = "diane 0411 234 567";
var phoneNumber7 = "bob";
if (phoneNumber1.match(phoneExpression)) {
console.log('Valid 10 digit mobile number');
}
if (phoneNumber2.match(phoneExpression)) {
console.log('Valid 8 digit landline number with circular brackets wrapping area code');
}
if (phoneNumber3.match(phoneExpression)) {
console.log('Valid 8 digit landline number with no spaces or area code');
}
if (phoneNumber4.match(phoneExpression)) {
console.log('Valid 10 digit mobile number with no spaces or international dialing code');
}
if (!phoneNumber5.match(phoneExpression)) {
console.log('Invalid landline number which is 6 digits instead of 8');
}
if (!phoneNumber6.match(phoneExpression)) {
console.log('A name and space before a valid spaced 10 digit mobile number');
}
if (!phoneNumber7.match(phoneExpression)) {
console.log('No valid number entered, a name appears to have been entered instead');
}
Supported formats: 0411 234 567, +61411 234 567, (02) 3892 1111, 38921111, 3892 111, 02 3892 1111
I haven’t encountered any issues using this expression, but it is possible there could be caveats using this. Like I said, my regular expression-fu isn’t very strong, but it works for what I needed it for and might work for you too.
Thank you so much for sharing your work. I was going to write one now you saved me a lot of time ๐ Can I use it for my project? Is there a licence associated with it?
Cheers,
Sheece,
No worries mate. Anything on this blog is free to use wherever, so go for it. No constraints or anything of the sort here ๐
Looks good but what bits do I leave out to force an area code?
Hi Dwayne, is this a typo in your footnotes?
“Supported formats: 0411 234 567, +61411 234 567, (02) 3892 1111, 38921111, 3892 111, 02 3892 1111”
Is “3892 111” a valid AU phone number or should this be “3892 1111”?
Thanks for sharing.
Hi Dwayne,
This is definitely a handy expression. Is there anyway you could break down the expression for others learning to write RE’s?
Or any learning tools you know of?
Thanks!
Hi Dwayne,
very good RE. I was finding the mobile only RE and landline only RE.
how this can be achieved .
Thanks and kind regards
Thanks Dwayne! You saved heaps of my time ๐
Hi guys,
Pretty regexp. However, be careful if you want to use it on a general database. Australia is full of 13… and 18… numbers. Many companies use at least a 13… number as a courtesy to customers. The above regular expression, as mouth watering as is, does not handle these numbers.
Thanks Dwayne, this is going to save me a ton of time ๐
Dwayne, How can we validate Mobile & Landline number separately? Can you provide two separate Regex for Mobile & Landline?
Thanks brother…..
{0,1} can be shortened to just ?. For example /a?/ will match 0 or 1 instances of the letter a. Also /[0-9]{1}/ is redundant. Just use /[0-9]/. It only matches one character unless you tell it to match more with * or +. You also shouldn’t need to escape spaces.
Thank you very much for this work and especially sharing with all.
It doesn’t support +61 411 234 567, which is also a very popular format people type the mobile number in.
Although this works, it’s not future proof. The main example I can think of is the plan for when all 04 numbers are exhausted is to start using 05 numbers, which your regex is going to say isn’t valid. I’d personally replace the (2|4|3|7|8) with [0-9] best to not have to run around changing and deploying new code because the government issues a new set of valid number.