0

I'd like to disallow the < character in first_name in our model. What would the regex for this be?

I tried

  validates :first_name, format: { with: /[^<]/ }

but doesn't seem to work.

timpone
  • 18,201
  • 33
  • 106
  • 205

2 Answers2

2

You can use without instead of with in the validation
You may want to escape the less-than, as zishe suggests, but I don't think this is necessary.

validates :first_name, format: { without: /</ }

https://guides.rubyonrails.org/active_record_validations.html#format

edit: incorporated @engineersmnky points, much simpler.

Matt
  • 13,577
  • 6
  • 43
  • 67
  • The "hat" inside a character class means any character **Not** in this class. However the OP's validation will only fail if the **Only** character is `" – engineersmnky Jan 18 '19 at 19:53
0

You are currently only capturing the first character. So you probably want to do something like [^<].* (a string starting with anything but < and followed by arbitrary characters)

inyono
  • 414
  • 2
  • 6