Javascript Regex - Valid Australian Phone Number

Need to validate an Australian Phone Number using Javascript? It could be easier than you think...

The other day I had to validate a form input which should be an Australian phone number. I'm sure I'm not alone when I say I find Regular Expressions a little confusing at times!

A few searches later and I was amazed at how complex the expressions needed to be, in order to account for any possible format that a user may enter data.

For example, our phone number could be entered as any of the following:

0897333194
08 9733 3194
(08) 9733 3194

And there are probably a lot more combinations.

What it boils down to though is that in a valid Australian phone number, there will never be any letters, there will always be 10 digits and the first digit will be 0.

Which makes the expression quite simply, the following:

/^\D*0(\D*\d){9}\D*$/

So in practice, you could use (attached to the onblur event but you could reference the value however you like):

if(!this.value.match(/^\D*0(\D*\d){9}\D*$/)) {alert('Phone Number is Invalid');}

Simple hey?