Posts tagged as:

validation

Valid email? Validate email addresses in Rails!

February 5, 2008

No Gravatar

Let’s face it, most of us creating apps collect at a very minimum a name and email address. By a simple validation method to our model, we can easily check the length, format, and presence of our fields. Checking the format of an email address is also simple, but requires a little more work. Using validates_format_of we are able to validate an email address against a regular expression (RegEx).

1
2
3
4
5
# our User model
class User < ActiveRecord::Base
  validates_presence_of :email
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
end

Read up on validations at the Rails API
Read up on regular expressions

{ Comments }