jqueryvalidation

bassistance validation help needed for australian phone numbers


I'm trying to use the jquery bassistance validator, which is working fine, but I need some help to add jquery to check if the numbers inputted are australian numbers (but I don't really know much about regex)

Here's the code for the UK phone:

jQuery.validator.addMethod("phoneUK",function(a,b){return this.optional(b)||a.length>9&&a.match(/^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})$/)},"Please specify a valid phone number");

And here's the code for the UK mobile:

jQuery.validator.addMethod("mobileUK",function(a,b){return this.optional(b)||a.length>9&&a.match(/^((0|\+44)7(5|6|7|8|9){1}\d{2}\s?\d{6})$/)},"Please specify a valid mobile number");

Australian landline phone numbers could be entered as 0X XXXX XXXX or 0XXXXXXXXX.

Australian mobile phone numbers could be entered as 04XX XXX XXX or 04XXXXXXXX.

Obviously the X's are numbers lol Any help would be appreciated, I need a similar code to the phoneUK and mobileUK but for AU numbers.. :)


Solution

  • Regex would be /^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$/

    And you'll want to change the a.length>9 to a.length>14 since something like (09) 9999 9999 would be a valid number.

    So, all together:

    jQuery.validator.addMethod("phoneAus",function(a,b){return this.optional(b)||a.length>14&&a.match(/^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$/)},"Please specify a valid mobile number");