views:

58

answers:

4

Hi all,

I have the following regular expression in a validation rule:

^[a-zA-Z0-9',!;?~>+&\"\-@#%*.\s]{1,1000}$

However, I can enter ====== which I believe should not be allowed.

My thoughts is that somehow the - could cause trouble if not properly escaped or something but this is way over my head.

+1  A: 
.

matches on everything. You want

\.
Dave Aaron Smith
Sure? It's inside these brackets: `[]`
Harmen
I'm also pretty sure that this is the problem.
levu
Uh, yeah, actually I'm completely wrong. Carry on :)
Dave Aaron Smith
+1  A: 

The - will be interpreted as a range indicator. You need to put it either first or last within the [] brackets if you want to match a literal -.

Bill the Lizard
+5  A: 

The regex you've shown us with the - escaped does not accept ===.
But if - is not escaped, === will be accepted. See this.

A - inside a regex is special and is used as range operator if it's not escaped and is surrounded by characters which participate as min and max in the range:

[a-z] matches any lowercase character.

[-az] matches either a - or a or z.

[az-] matches either a - or a or z.

[a\-z] matches either a - or a or z.

[a-c-d-f] matches a or b or c or - or d or e or f. The first and last - act as range operator but the one in the middle is treated literally.

In your case the = comes in the range "-@ and hence gets matched.

codaddict
Thank you very much :)
Eton B.
+1 for answering a non-question.
Brad
A: 

Your regex works fine for me. but if i remove the escaping of - it matches =. I'm sure yo uare doing that.