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.
That will match true if the string ends with a valid character. No validation on anything in the middle. Try this:
^[a-zA-Z0-9]*$
You need to anchor the pattern on both sides:
/^[a-zA-Z0-9]+$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With