Viet
Viet

Reputation: 3387

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.

Upvotes: 6

Views: 4734

Answers (1)

pguardiario
pguardiario

Reputation: 54984

You want to 'look ahead' for a letter:

/\A(?=.*[a-z])[a-z\d]+\Z/i

Upvotes: 13

Related Questions