Reputation: 18871
I am using Ruby on Rails 3.0.9 and I would like to validate a string that can contain only characters (case insensitive characters), blank spaces and numbers.
More:
-
and _
;The regex that I know from this question is ^[a-zA-Z\d\s]*$
but this do not validate special characters and accented characters.
So, how I should improve the regex?
Upvotes: 3
Views: 8623
Reputation: 1
Validation string only for not allowed characters. In this case |,<,>," and &.
^[^|<>\"&]*$
Upvotes: 0
Reputation: 30715
I wrote the ^(?:[^\W_]|\s)*$
answer in the question you referred to (which actually would have been different if I'd known you wanted to allow _ and -). Not being a Ruby guy myself, I didn't realize that Ruby defaults to not using Unicode for regex matching.
Sorry for my lack of Ruby experience. What you want to do is use the u
flag. That switches to Unicode (UTF-8), so accented characters are caught. Here's the pattern you want:
^[\w\s-]*$
And here it is in action at Rubular. This should do the trick, I think.
The u
flag works on my original answer as well, though that one isn't meant to allow _ or - characters.
Upvotes: 3
Reputation: 9226
Something like ^[\w\s\-]*$
should validate characters, blank spaces, minus, and underscore.
Upvotes: 0