Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby, Rails - how to check if a var is an Email

Does Rails 3 or Ruby have a built in way to return true or false on if a var is an EMAIL ADDRESS?

example

1 returns false
[email protected] returns true
[email protected] returns true
asdasdasdads returns false

Maybe something like, if 1.is_a? Email end

thanks

like image 953
AnApprentice Avatar asked Nov 26 '25 06:11

AnApprentice


2 Answers

I ended up using the Rails gem, EmailVeracity which worked great

like image 166
AnApprentice Avatar answered Nov 27 '25 20:11

AnApprentice


Something like this should work:

def isEmail(str)
  return str.match(/[a-zA-Z0-9._%]@(?:[a-zA-Z0-9]\.)[a-zA-Z]{2,4}/)
end
like image 42
Austin M Avatar answered Nov 27 '25 19:11

Austin M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!