I'm currently using the jQuery validation Engine with jQuery 1.10. After peeking into the validation script itself (which, for phone numbers is a custom type), I noticed that it utilizes the following regex:
/^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/
What I'm trying to do is also allow "TEXT" and "TEXTTWO" as acceptable values. I've tried:
/^?(TEXT|TEXTTWO)^?([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/
...and several variations on the theme without success.
Good for you for attempting! But it seems there's just one thing you're misunderstanding: ^
is an anchor—it matches the beginning of a string—it cannot be quantified, e.g. made optional via ?
, nor does it make sense to appear after some part of the string has already been matched, since then it could definitely not be the beginning of the string. Try this instead:
/^TEXT|TEXTTWO|([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/
So, you had the right idea with alternation (|
). Note that alternation has one of the lowest precedence of all operators in regex, so no grouping is necessary here.