wheresdave
2007-Jul-02 08:25 UTC
Anyone have an idea why this regex wont validate email addresses?
Just purchased the book RailsSpace (Rough Cut Version) and I am going
through the tutorial for the user model. It says to add the code below
for email validation to your user model, which I did.
It then tells you to create a username email and password in IRB to
test it which i did. But each time it says the email is not a valid
email even though i have tried 5 or 6 different emails etc.
Went through the code with a fine toothed comb, tried a couple other
regex''s i found on google, but to no avail.
Anyone take a look at this for me, and see if they spot anything
amiss?
Thanx,
Dave
class User < ActiveRecord::Base
# Max & min lengths for all fields
SCREEN_NAME_MIN_LENGTH = 4
SCREEN_NAME_MAX_LENGTH = 20
PASSWORD_MIN_LENGTH = 4
PASSWORD_MAX_LENGTH = 40
EMAIL_MAX_LENGTH = 50
SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGTH..SCREEN_NAME_MAX_LENGTH
PASSWORD_RANGE = PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH
validates_uniqueness_of :screen_name, :email
validates_length_of :screen_name, :within => SCREEN_NAME_RANGE
validates_length_of :password, :within => PASSWORD_RANGE
validates_length_of :email, :maximum => EMAIL_MAX_LENGTH
validates_format_of :screen_name,
:with => /^[A-Z0-9_]*$/i,
:message => "must contain only letters, " +
"numbers, and underscores"
validates_format_of :email,
:with => /^[A-Z0-9._%-]+@([A-Z0-9-]+\.)+[A-Z]
{2,4}$/i,
:message => "must be a valid email address"
def validate
errors.add(:email, "must be valid.") unless
email.include?("@")
errors.add(:screen_name, "cannot include spaces.") if
screen_name.include?(" ")
end
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
wheresdave
2007-Jul-02 08:50 UTC
Re: Anyone have an idea why this regex wont validate email addresses?
fixed it. Guess i needed to restart Instant_rails. On Jul 2, 1:25 am, wheresdave <wheresd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Just purchased the book RailsSpace (Rough Cut Version) and I am going > through the tutorial for the user model. It says to add the code below > for email validation to your user model, which I did. > > It then tells you to create a username email and password in IRB to > test it which i did. But each time it says the email is not a valid > email even though i have tried 5 or 6 different emails etc. > > Went through the code with a fine toothed comb, tried a couple other > regex''s i found on google, but to no avail. > > Anyone take a look at this for me, and see if they spot anything > amiss? > > Thanx, > > Dave > > class User < ActiveRecord::Base > > # Max & min lengths for all fields > SCREEN_NAME_MIN_LENGTH = 4 > SCREEN_NAME_MAX_LENGTH = 20 > PASSWORD_MIN_LENGTH = 4 > PASSWORD_MAX_LENGTH = 40 > EMAIL_MAX_LENGTH = 50 > SCREEN_NAME_RANGE = SCREEN_NAME_MIN_LENGTH..SCREEN_NAME_MAX_LENGTH > PASSWORD_RANGE = PASSWORD_MIN_LENGTH..PASSWORD_MAX_LENGTH > > validates_uniqueness_of :screen_name, :email > validates_length_of :screen_name, :within => SCREEN_NAME_RANGE > validates_length_of :password, :within => PASSWORD_RANGE > validates_length_of :email, :maximum => EMAIL_MAX_LENGTH > > validates_format_of :screen_name, > :with => /^[A-Z0-9_]*$/i, > :message => "must contain only letters, " + > "numbers, and underscores" > validates_format_of :email, > :with => /^[A-Z0-9._%-]+@([A-Z0-9-]+\.)+[A-Z] > {2,4}$/i, > :message => "must be a valid email address" > > def validate > errors.add(:email, "must be valid.") unless > email.include?("@") > errors.add(:screen_name, "cannot include spaces.") if > screen_name.include?(" ") > end > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---