Reputation: 651
How to validate text input so that it only allows a-z, A-Z, 0-9, ();:?!,.[]{}-
characters(also space)?
I added something like in my model
class Oder < ApplicationRecord
......
......
validates :text, presence: true, format: { with: /[0-9\w]*[\(\;\:\-\?\!\,\.\[\]\{\}\,\.)]*/ }
......
end
end
But it is not working. It allows other characters(wïth ûmlauts) also. Thanks in advance.
Upvotes: 0
Views: 366
Reputation: 106932
At the moment the regexp matches when one or more characters are in your text somewhere. You need to change the regexp to only match when the characters are the only characters between the start of the string (\A
) and its end (\z
).
Change your regexp to:
/\A[0-9\w\s\(\;\:\-\?\!\,\.\[\]\{\}\,\.)]*\z/
Upvotes: 4