6

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.

1 Answer 1

13

You want to 'look ahead' for a letter:

/\A(?=.*[a-z])[a-z\d]+\Z/i
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.