Mano
Mano

Reputation: 977

validation for multipe email addresses

For inviting the new user by giving their mail addresses in text field(multipe email addresses), i need to validate the given email addresses format and need to show the proper error message.Here i am using the space between the each email addresses.(for example [email protected] [email protected])Please guide me.

1)How to validate user email addresses format?.

2)How to check if the user doesn't use space between the two email addresses?.

3)Suggest some best way to do this.

i am using rails(2.3.X and ruby 1.8.7)

User controller

def invite_users
  if request.get?
    #render invite_users.html.erb
  elsif request.post?
    if !params[:email_ids].blank? && !params[:message].blank?
      @email_ids = params[:email_ids].split(/ |, |,/)
      @message = params[:message]
      @inviting_users = @email_ids.count
      @inviting_users.times do
        @all_emails = []
        @all_emails = @email_ids.shift
        activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join)
        UserNotifier.deliver_invite_users(@all_emails, @message,activation_code, current_company)
        end
     else
       flash[:notice] = "Please fill all the fields"
     end
  end
end

Upvotes: 3

Views: 183

Answers (1)

user740316
user740316

Reputation:

1) You could use a simple regular expression to check the value of the email field. There are many regular expressions which may work, from the most laxist to the most über-complex one. People generally tend to use something like this :

unless email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
   puts "Your email address does not appear to be valid")

This regular expression may let pass some invalid email adresses (only very very tricky ones, so basically you can consider that this regular expression is enough in 99.99% of the time). If you really want a (almost) flawless checking, you may consider this one : http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html (I don't know if you need that much though).

For more regular expressions to validate email adresses (and simpler than my last one of course), please take a look here : Using a regular expression to validate an email address

2) Just strip() the field where both e-mail adresses are entered.

Upvotes: 2

Related Questions