I am currently implementing the following:
phone_number.match(/^((\+44\s?|0)7([45789]\d{2}|624)\s?\d{3}\s?\d{3})$/);
In most cases, the above code works well. Valid formats include:
07714000000 +447714000000
However, I also need to allow the format without the plus sign, for example:
447714000000
I tried adding a "?" between the "+" and "44", which worked in an online tester but not in JavaScript.
How can I achieve this?
Thank you
Update I am using this in the JQuery Validation plugin, here is the code:
jQuery.validator.addMethod('mobileUK', function(phone_number, element) {
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^((\+?44\s?|0)7([45789]\d{2}|624)\s?\d{3}\s?\d{3})$/);
}, 'Please specify a valid mobile number');
It functions normally without the "?", but does not match with just "44".