0

I have a simple CLI, something like:

p 'Enter email value'
params[:email] = gets.chomp

and I want to handle email validation and if the email is invalid then rerun the previous prompt.

I have method for validating email, that simply returns true or false, something like:

p 'Enter email value'
params[:email] = gets.chomp
if email invalid
  'error- invalid email'

and again run

p 'Enter email value'
params[:email] = gets.chomp

How can I do it?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user
  • 1,341
  • 2
  • 17
  • 28
  • Please use proper capitalization and grammar when asking. SO isn't a discussion list where anything goes, it's an online reference book where grammar counts. – the Tin Man Apr 19 '17 at 22:58
  • Do you want to determine if an email address is valid according to the RFC rules, or only whether it's a deliverable address? The first takes a very non-trivial pattern. The second is best done by trying to send an email to that address requesting a response. http://stackoverflow.com/a/201378/128421 will help. – the Tin Man Apr 19 '17 at 23:04

2 Answers2

0

You can do this with Regular Expressions.

To validate whether the email is a valid one or not, you can use regex like so:

def validate_email
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  if email =~ VALID_EMAIL_REGEX
    # Do someting here
  else
    validate_email
  end
end
Carter Brainerd
  • 199
  • 2
  • 10
0

You can enter this code into until loop

p 'Enter email value'
params[:email] = gets.chomp
until email invalid do
        p 'Enter email value'
        params[:email] = gets.chomp
end

Read more at: https://www.tutorialspoint.com/ruby/ruby_loops.htm

Dori Aviram
  • 292
  • 2
  • 8