Skip to content
Blog

Regular Expression for Email Validation in JavaScript

By Published A short read

     Hello everyone, this article we are going to implement regular expression for email validation in Javascript. Regular Expression is used to validate some value to a specific pattern. If the pattern matches the input value that means it is true else false.

Prefer to watch? Here's the video version of this post.

    This is not limited to javascript you can implement regular expression in any programming language. The only difference that you will notice is the methods or functions which are built-in the programming language to match these regular expression patterns.

    In javascript itself there are many ways to test it. And in this example we will use test() function to test the regular expression against some value pass in as arguments.

Regular Expression For Email Validation in Javascript 

code:

let regx =
    /^[_A-z0-9-]+(\.[_A-z0-9]+)*@[A-z0-9]+(\.[A-z0-9-]+)*(\.[A-z]{2,3})$/
console.log(regx.test("sample@email.com"))
console.log(regx.test("sample.sample@email.subdomain.com"))
console.log(regx.test("sample@email.com"))
console.log(regx.test("sample.sample@email..com"))
console.log(regx.test("email.com"))
console.log(regx.test("sample@gmail"))
console.log(regx.test("sample@gmail."))
console.log(regx.test("sample@."))
console.log(regx.test("myEmail"))
console.log(regx.test(" "))
console.log(regx.test("#@%^%#$@#$@#.com"))
console.log(regx.test("@example.com"))

output:

$ node email.js

true

true

true

false

false

false

false

false

false

false

false

false