Prohibit ampersand in Rails form

Posted by snlsn on Stack Overflow See other posts from Stack Overflow or by snlsn
Published on 2010-05-07T02:34:21Z Indexed on 2010/05/07 2:38 UTC
Read the original article Hit count: 204

NOT a Rails 3 issue

In a Contact model I have a company_name attribute. For reasons that don't matter to this question, I want to prohibit an ampersand character. We have a lot of clients with ampersands in their company name, and users forget they aren't allowed to use this character.

This isn't an html sanitize issue. I don't care about whitespace or CDATA or anything. The entries in this field are plain text and I don't want an ampersand to be entered in this one field in any way, shape or form.

I assume a validation on the model is the way to go. I have tried validates_exclusion_of. I have tried validates_format_of. No success. I'm unsophisticated when it comes to regex, so I might be doing things very wrong. But the bottom line is - I need to prevent a user from entering that "&" character in the company_name field.

Thanks a million.

Steve

© Stack Overflow or respective owner

Related posts about rails

Related posts about regex