Vanilla JavaScript Email Validation

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today I want to address a topic I use quite often but noticed I've never written about. Email validation! Since my day job is in marketing, we build a lot of pages with forms, and the least we need is an email address. So how do we make sure the input is at least a valid email type in JavaScript?
For today's work we'll use a very simple form, with only a email input and a button to submit. Then we'll have a response div to show us if the email was correct.
<div class="container">
<input type="email" id="emailField" />
<br />
<button id="button">Check validation</button>
<div id="response"></div>
</div>
Ok, now on to the fun part, JavaScript! Let's start by defining our variables we need to use.
var emailField = document.getElmentById('emailField');
var button = document.getElementById('button');
var response = document.getElementById('response');
Awesome, very basic selectors, but enough for this excercise.
Now we want to add a click listener to our button.
button.addEventListener('click', function() {
var email = emailField.value;
console.log(email);
});
This function will log the value of what we put in the input field.
So for now let's make our actual function to check if it's valid:
function validateEmail(email) {
const 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(String(email).toLowerCase());
}
BAM! Please don't be scared; it's a plain old regular expression we are using. This will validate a valid email format.
It will return true or false, depending on the state.
So let's implement this back in our button click.
button.addEventListener('click', function() {
var email = emailField.value;
if (validateEmail(email)) {
response.innerHTML = 'Hiya Cowboy, this email will work for us 🤠';
} else {
response.innerHTML = 'Sorry, this email is not cool enough 😩';
}
});
There we go! Of course, you would like to do something with this information, but I'll leave that up to you!
View this example on the following Codepen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter