Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails format validation -- alphanumeric, but not purely numeric

Whats the best way to test a format validation of lets says a username, with a regex for alphanumeric, but not purely numeric?

I've been using the following validation in my model

validates :username, :format => { :with => /^[a-z0-9]+[-a-z0-9]*[a-z0-9]+$/i }

Numeric username's such as '342' pass the validation, which I don't want.

like image 812
Viet Avatar asked Dec 16 '12 00:12

Viet


1 Answers

You want to 'look ahead' for a letter:

/\A(?=.*[a-z])[a-z\d]+\Z/i
like image 64
pguardiario Avatar answered Nov 07 '22 01:11

pguardiario