ruby on rails - Regexp in format: validation isn't stopping the right characters -


i'm new regex , i'm trying allows letters, numbers, quotes , following characters: !.:?!_+=, -

i have validation below in guide modal. can name guide '&' if want , accepts it, understanding validation should stop me.

validates :name,  presence: true, length: { maximum: 255 }, uniqueness: { case_sensitive: false }, format: { with: /[a-za-z 1-9 0!.'":?!_+=, -]/, message: "only allows letters, numbers, quotes , !.:?!_+=, -" } 

not sure going wrong, tested out regex in rubular.com , works in there.

edit

after testing it, turns out stops '&' lets '&11' pass. if invalid key valid key passes. maybe i'm using format: wrong or shouldn't using format: this?

i've simplified bit regex, added proper starting/ending line markers , added + @ end match if there 1 or more char in input. here's looks like:

/\a[a-za-z0-9 !.'":?!_+=,-]+\z/ 

if want have cleaner one:

/\a[\w !.'":?!_+=,-]+\z/ 

since \w matches [a-za-z0-9]


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -