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 jq开发者_StackOverflow社区uery 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.. :)
/^[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}$/
{1} is the default, no need to specify it at all.
Replace [0-9] with \d so that [x-y] is used only when there's a reduced range.
[ ] simplifies to \s
Having found the start, don't keep finding it again: ^...$|^...$|^...$|^...$ simplies to ^(...|...|...|...)$
/^(\d{8}(\d{2})?|\(0[1-9]\)\d{8}|\d{4}\s\d{3}\s\d{3}$|(\(0[1-9]\))?\s\d{4}\s\d{4})$/
Even the above simplified pattern has redundant parts and overlaps.
/^(\d{8}(\d{2})?|\d{4}\s\d{3}\s\d{3}$|(\(0[1-9]\))?\s?\d{4}\s?\d{4})$/
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");
精彩评论