0

I have 6 email address like below:

(1) ali@hotmail.com -> valid
(2) @hotmail.com -> invalid
(3) ali@hotmail.com, abu@hotmail.com -> valid
(4) ali@hotmail.com, @hotmail.com -> invalid
(5) ali@hotmail.com, abu@hotmail.com, ahmad@hotmail.com -> valid
(6) ali@hotmail.com, @hotmail.com, ahmad@hotmail.com -> invalid

How do I use JavaScript to determine that the email address is in full format?
I try startsWith("@hotmail.com"), endsWith("hotmail.com"), indexOf("@hotmail.com") also cannot fulfill all the email addresses above.
Can someone help me?

Michael Kuan
  • 1,085
  • 3
  • 10
  • 28
  • Have you considered doing a regular expressions tutorial? Otherwise, first split the input on commas to get an array of individual addresses to test. Then spell out the rules you want to apply in plain English, so that you yourself understand what you are trying to do, and then you'll probably find it fairly easy to implement each rule in JS. E.g. "the index of the @ must be >= 1 (because that means there is at least one character before the @)". – nnnnnn Mar 22 '16 at 03:25

2 Answers2

0

Using regular expressions is probably the best way. Pass the string in this function and this function will return true of false

function validateEmail(email) {
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|    (".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
koolhuman
  • 1,581
  • 15
  • 25
  • 3
    Seems like you just copied the code from http://stackoverflow.com/questions/46155/validate-email-address-in-javascript without even giving contribution. Don't copy other SO answers. If an existing answer solves the issue, flag/vote to close as duplicate. – Felix Kling Mar 22 '16 at 03:27
  • Does this validate the examples in the question where the input includes multiple addresses separated by commas (and spaces)? – nnnnnn Mar 22 '16 at 03:29
  • cool. I am not sure how to flag a question as duplicate. Also i am not sure if i have privilege to mark question as duplicate. – koolhuman Mar 22 '16 at 03:30
  • @koolhuman so rather than linking your answer to the answer you copied you just want to try take full credit for it? People might down vote you for that. If you can't flag questions as duplicate then it's because you haven't earned that privilege yet and copy/paste other answers isn't a good way to go about earning privileges. – NewToJS Mar 22 '16 at 03:38
0

You could use regex

For example,

/[A-Za-z0-9-]+@hotmail.com/
Elipzer
  • 901
  • 6
  • 22