validate an email address from a form field - version 2

This javascript function will check if the email address in the text box has an @ and a . in it and return a message saying if its valid or not. 

This is pretty basic as it only includes checking if the string has an @ and a . but not the position of them. 

HTML

<input type="email" id="email_check"> <button onclick="validate_email();" class="btn btn-primary">Validate Email</button>

Scripts

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

Javascript

function validate_email() {
  	var emailfield = document.getElementById('email_check').value;
    //alert(emailfield);
  
    // does the string have an @?
    if(!emailfield.includes("@")) {
      alert("Missing the @");
      return false;
    }

      // does the string have a .?
    if(!emailfield.includes(".")) {
      alert("Missing the .");
      return false;
    }
  
    // if we get to here, then the string has an @ and a dot, 
    // the email address could still look like this though...
    // @@@@......@@@@@
    // which is not valid, but its still better than nothing...
  
  	alert("Email appears to be valid");
  	return true;
  
}

External Link for validate an email address from a form field - version 2

Other Items in javascript

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 114 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

âĪðŸ‘Đ‍ðŸ’ŧðŸ•đ

Random Quote
I'm a big believer in energy and the secret and that sort of thing.
Unknown