• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

Regular Expression For Validating Australian Phone Numbers (Including landline and mobile)

Javascript · August 1, 2014

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.

Dwayne

Leave a Reply Cancel reply

15 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Sheece
Sheece
5 years ago

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,

0
Dwayne
Dwayne
Author
5 years ago

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 ๐Ÿ™‚

2
Dave Stott
Dave Stott
5 years ago

Looks good but what bits do I leave out to force an area code?

0
Troy
Troy
5 years ago

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.

0
Clinton
Clinton
5 years ago

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!

0
jalil
jalil
5 years ago

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

0
ozzman
ozzman
5 years ago

Thanks Dwayne! You saved heaps of my time ๐Ÿ™‚

0
Zoltan Mohos
Zoltan Mohos
5 years ago

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.

0
Philip
Philip
4 years ago

Thanks Dwayne, this is going to save me a ton of time ๐Ÿ™‚

0
Mehul
Mehul
4 years ago

Dwayne, How can we validate Mobile & Landline number separately? Can you provide two separate Regex for Mobile & Landline?

0
rohit
rohit
4 years ago

Thanks brother…..

0
3ocene
3ocene
3 years ago

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

0
Krunal Shah
Krunal Shah
2 years ago

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.

0
C
C
1 year ago

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.

0
trackback
Regular Expression For Validating Australian Phone Numbers (Including landline and mobile) - Glenn Eaton
1 year ago

[…] https://ilikekillnerds.com/2014/08/regular-expression-for-validating-australian-phone-numbers-includ…, viewed […]

0

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript
  • How To Mock uuid In Jest
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • NBN Box Installed Inside of Garage, Where Do You Put The Modem?
  • How to Copy Files Using the Copy Webpack Plugin (without copying the entire folder structure)

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 ยท Dwayne Charrington ยท Log in

wpDiscuz