0

I'm trying to create a form that verifies that an email address has been entered. Not necessarily check its validity but basically sakjfsfksldjf@email.com. I was wondering how to go about doing it in PURE JavaScript and no RegEx.

    <!DOCTYPE html>

    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>Test File</title>
    </head>
        <script>
            function submitForms() {
                email_format = document.getElementById('email_input')       // want to return T or F
                if (email_format) {
                    //print email successful
                }
                else {
                   //print error message
                }         
        </script>

    <body>

        <form>
            Email:            
            <input type ="email" id ="email_input" />
        </form>

        <button type = "button" onclick = "submitForms;"> Submit All!
        </button>
    </body>
    </html>
Liondancer
  • 15,721
  • 51
  • 149
  • 255

3 Answers3

1

This has been answered a lot. Here's a good function that tests for a valid email format and it's not too tricky to read, and not too restrictive either.

function IsEmail(email) {
    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    return regex.test(email);
}

Per: Email validation using jQuery

Community
  • 1
  • 1
Matt Pavelle
  • 819
  • 5
  • 8
1

Here is a previously answered regular expression answer. Its pretty good.

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);
} 

Credits: https://stackoverflow.com/a/46181/295264

However, the only good to verify an email address is by sending a confirmation email and hearing back.

Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
1

I think you are looking for this:

 function IsEmail(email) {
 var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 return regex.test(email);
}

which was found here: Email validation using jQuery

Community
  • 1
  • 1
bart2puck
  • 2,432
  • 3
  • 27
  • 53