From 67344d73a7a3644df695ad64d4714b4545694f04 Mon Sep 17 00:00:00 2001 From: Robert Stevenson Date: Sun, 19 Dec 2010 04:39:38 +0800 Subject: [PATCH] Added a more thorough email format validation regex for User --- lib/clearance/user.rb | 2 +- test/models/user_test.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/clearance/user.rb b/lib/clearance/user.rb index 3b9925cc8..ddf074d86 100644 --- a/lib/clearance/user.rb +++ b/lib/clearance/user.rb @@ -50,7 +50,7 @@ def self.included(model) model.class_eval do validates_presence_of :email, :unless => :email_optional? validates_uniqueness_of :email, :case_sensitive => false, :allow_blank => true - validates_format_of :email, :with => %r{.+@.+\..+}, :allow_blank => true + validates_format_of :email, :with => %r{^[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#\$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$}i, :allow_blank => true validates_presence_of :password, :unless => :password_optional? validates_confirmation_of :password diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 3a83b4b32..389280e55 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -12,7 +12,11 @@ class UserTest < ActiveSupport::TestCase context "When signing up" do should validate_presence_of(:email) should validate_presence_of(:password) + should allow_value("foo@example.co.uk").for(:email) should allow_value("foo@example.com").for(:email) + should_not allow_value("foo@").for(:email) + should_not allow_value("foo@example..com").for(:email) + should_not allow_value("foo@.example.com").for(:email) should_not allow_value("foo").for(:email) should_not allow_value("example.com").for(:email)