pynovice
pynovice

Reputation: 7752

Modify this regex to accept 0 to 15 characters?

I have a regex like this:

var filter = /^[ A-Za-z0-9_@./#&+-]*$/;

I want this regex to return true if there is no character on input field or if there is character less than 14. I tried using this:

var filter = /^[ A-Za-z0-9_@./#&+-]{0, 15}*$/;

But this regex is never returning true. It always return false even after I satisfy the condition. What's wrong?

Upvotes: 1

Views: 536

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336308

Whitespace is significant in a regular expression. The space character makes your {n,m} quantifier invalid, causing {0, 15}* to be evaluated as the literal string "{0, 15", followed by zero or more }s.

Also, you can condense [A-Za-z0-9_] into \w:

var filter = /^[ \w@./#&+-]{0,15}$/;

Upvotes: 3

Ganesh Rengarajan
Ganesh Rengarajan

Reputation: 2006

Try this one.........

^\w{1,15}$

Upvotes: 0

Related Questions