Regular expression that calls string "a?c" invalid?

regex, validation

Solution

You need to anchor the pattern on both sides:

/^[a-zA-Z0-9]+$/

Problem

In my user model, I have an attribute called "nickname" and validates as such: validates_format_of :nickname, :with => /[a-zA-Z0-9]$/, :allow_nil => true However, it is currently letting this string pass as valid: a?c I only want to accept alphanumeric strings - does anyone know why my regular expression is failing? If anybody could suggest a better regular expression, I'm all ears.

Original source