8

Validation of comma separated e-mail

 2 years ago
source link: https://www.codesd.com/item/validation-of-comma-separated-e-mail.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Validation of comma separated e-mail

advertisements

I am using the following link for validating email: http://jquerybyexample.blogspot.com/2011/04/validate-email-address-using-jquery.html

I am using this function to validate my email address using js:

validateEmail = (sEmail) ->
  filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
  if filter.test(sEmail)
    true
  else
    false
$(document).ready (e) ->
  $("#invitation_form").submit ->
    sEmail = $("#invitation_email").val().split(',')
    email=0
    for email in [0..sEmail.length]
      if $.trim(email).length is 0
        $("h2").append("<div class='alert alert-error'>Please review the problems below</div>")
        $("#invitation_email").attr("placeholder", "Email can't be blank")
        return false
      if validateEmail(email)
      else
        alert sEmail
        email++
        # $("h2").append("<div class='alert alert-error'>Please review the problems below</div>");
        # $("#invitation_email").val('')
        # $("#invitation_email").attr("placeholder", "Please enter valid email")
        # return false

This is validating my email properly if I put only one email. But in my email text field I have to put many comma separated emails and then validate each email individually. For that I had put split(','), and the added the for loop but the validation is not done properly. If I put '[email protected], [email protected]', then its going in the else block which is for invalid emails. In the alert I am getting the individual emails but not getting how to validate each email individually. Can someone please help me in this? Thanks in advance.


When you say this:

for email in [0..sEmail.length]

you're saying:

for email in an_array_of_numbers

so the email values inside that loop will be integers and integers aren't email addresses. You say that:

This is validating my email properly if I put only one email.

but that's not true, it fails with one email address or many as you can see in this demo:

http://jsfiddle.net/ambiguous/7LFus/

If you want to look at the email addresses in sEmail, then you want:

for i in [0...sEmail.length]
  email = sEmail[i]
  #...

or better:

for email in sEmail
  #...

Note that the first version uses ... instead of your .., .. includes the upper limit so if sEmail.length is one you'd be iterating over [0, 1] rather than the [0] that would match the array's indexes. I'd go with the for email in sEmail version rather than worrying about indexes.

Also, String#split can take a regex as the splitting pattern so you could removing the leading and trailing whitespace while splitting:

sEmail = $("#invitation_email").val().split(/\s*,\s*/)

That way you don't have to $.trim inside the loop and you won't forget to trim when calling validateEmail.

While I'm here, Regexp#test returns true or false so you don't need to say:

if filter.test(sEmail)
  true
else
  false

you can simply say:

filter.test(sEmail)

So just a couple small changes and you should have something that is cleaner and works better.

Demo: http://jsfiddle.net/ambiguous/SzzXV/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK