Matt
Matt

Reputation: 11

Invalid quantifer error using Regular Expression (UK Telephone numbers)

I am getting the error "Invalid Quantifier" when using this regex:

^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$

Infact ive tried a few UK telephone number regex's from the regex librairy but im getting the same error all the time. If anyone can help id be much appreciative!

Upvotes: 1

Views: 397

Answers (2)

powtac
powtac

Reputation: 41040

Use this, its from http://www.regexlib.com/Search.aspx?k=uk%20tele

(\s*\(?0\d{4}\)?(\s*|-)\d{3}(\s*|-)\d{3}\s*)|(\s*\(?0\d{3}\)?(\s*|-)\d{3}(\s*|-)\d{4}\s*)|(\s*(7|8)(\d{7}|\d{3}(\-|\s{1})\d{4})\s*)

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146420

You get the same error if you simply run this:

new RegExp("^(((+44\s?\d{4}|(?0\d{4})?)\s?\d{3}\s?\d{3})|((+44\s?\d{3}|(?0\d{3})?)\s?\d{3}\s?\d{4})|((+44\s?\d{2}|(?0\d{2})?)\s?\d{4}\s?\d{4}))(\s?#(\d{4}|\d{3}))?$");

... so you'd better forget about jQuery and Form Validator until you get the regexp right.

The JavaScript console says this:

Error: invalid quantifier +44s?d{4}|(?0d{4})?)s?d{3}s?d{3})|((+44s?d{3}|(?0d{3})?)s?d{3}s?d{4})|((+44s?d{2}|(?0d{2})?)s?d{4}s?d{4}))(s?#(d{4}|d{3}))?$

The + quantifier means one or more is used to modify a previous rule, e.g.:

A+ --> One or more A's
\d+ --> One or more digits

So you need something to quantify:

(((+ --> Nothing to modify

Upvotes: 1

Related Questions