Skip to content

Email validation in js | Html Code example

  • by

Email validation in JS required when you want input from users. Like account login needed an email id. An email id data will be sent to the server where it will check with the database. But you can do HTML input validation of email id text format are Right or wrong using Regular expression and JavaScript.

You can validate the HTML form on the client-side so data processing will be faster than server-side validation.

Email validation in JS Examples

Email validation regex, Using regular expressions is probably the best way. You can see below example code:-

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

Here’s an complete example of the above in action:

<!DOCTYPE html>
	<script src=""></script>


			<p>Enter an email address:</p>
			<input id='email'>
			<button type='submit' id='validate'>Validate!</button>

		<h2 id='result'></h2>

	<script type="text/javascript">
		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(email);

		function validate() {
			const $result = $("#result");
			const email = $("#email").val();

			if (validateEmail(email)) {
				$result.text(email + " is valid :)");
				$result.css("color", "green");
			} else {
				$result.text(email + " is not valid :(");
				$result.css("color", "red");
			return false;

		$("#validate").on("click", validate);



Email validation in js example

HTML email validation no need js

You don’t actually need JavaScript for this at all with HTML 5; just use the email input type:

<input type="email">

If you want to make it mandatory, you can add the required parameter.

If you want to add additional RegEx validation (limit to email addresses for example), you can use the pattern parameter, e.g.:

<input type="email" pattern="[email protected]">

Q: How to Email id validation in JavaScript without regular expression?

Answers: You can do it like below example.

<!DOCTYPE html>
        <script type="text/javascript">
       function testEmailAddress(emailToTest) {
    // check for @
    var atSymbol = emailToTest.indexOf("@");
    if(atSymbol < 1) return false;

    var dot = emailToTest.indexOf(".");
    if(dot <= atSymbol + 2) return false;

    // check that the dot is not at the end
    if (dot === emailToTest.length - 1) return false;

    return true;
	alert(testEmailAddress("[email protected]"))
    <body onload="codeAddress();">


Email id validation in JavaScript without regular expression

Do comment if you have any questions and suggestion on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading