# Anyone have an idea why this regex wont validate email addre

**URL:** <https://www.ruby-forum.com/t/anyone-have-an-idea-why-this-regex-wont-validate-email-addre/108323>\
**Category:** Rails\
**Created:** [July 2, 2007, 10:27am UTC](https://www.ruby-forum.com/t/anyone-have-an-idea-why-this-regex-wont-validate-email-addre/108323 "2007-07-02T10:27:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wheresdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6a8cbe/32.png) [@wheresdave](https://www.ruby-forum.com/u/wheresdave)\
**Post date:** [July 2, 2007, 10:27am UTC](https://www.ruby-forum.com/t/anyone-have-an-idea-why-this-regex-wont-validate-email-addre/108323/1 "2007-07-02T10:27:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wheresdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/6a8cbe/32.png) [@wheresdave](https://www.ruby-forum.com/u/wheresdave)\
**Post date:** [July 2, 2007, 10:52am UTC](https://www.ruby-forum.com/t/anyone-have-an-idea-why-this-regex-wont-validate-email-addre/108323/2 "2007-07-02T10:52:17Z")

</div>

fixed it. Guess i needed to restart Instant\_rails.
