I was giving this regex /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]{2,}\z/ and I'm to use it in order to verify the validity of an email address.
This is my first Ruby code and I'm not sure how to do it. I was told to use the .match method, which returns MatchData object. But how do I go on verifying that the MatchData object confirms the validity?
I attempted using the following, but it seems that it's accepting any string, even not an email address.
#Register new handler
def register_handler
email_regex = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]{2,}\z/
email = "invalid"
unless email =~ email_regex then
puts("Insert your e-mail address:")
email = gets
end
puts("email received")
end
What is the correct way to do this? Either using .match or the method I attempted above.