1

I have this regex in my model post:

validates_format_of :description, :username, :with => /^(?:[^\W_]|\s)*$/u, :message => "should only contain letters, numbers, or .,-_@"

But this regex does not allow dots "." and commas ",".

I want allow add this characters to this regex.

How can I allow that this regex works fine for fields like username or textareas for validate letters, numbers, commas, dots...etc

Phrogz
  • 296,393
  • 112
  • 651
  • 745
hyperrjas
  • 10,666
  • 25
  • 99
  • 198
  • 2
    Note that in Ruby, `^` and `$` mean "start of line" and "end of line" rather than "start of string" and "end of string"; so your regex actually matches any string that contains *at least one line* with only letters and numbers and whitespace. – ruakh Jan 19 '12 at 16:01
  • What @ruakh says is important, you'll want to use `\A` and `\Z` to anchor to the beginning and end of the string – Gareth Jan 19 '12 at 19:11

1 Answers1

2
validates_format_of :description, :username, :with => /^(?:[^\W_]|\s|[\.,_@])*$/u, :message => "should only contain letters, numbers, or .,-_@"
PinnyM
  • 35,165
  • 3
  • 73
  • 81