Try the Introductory JavaScript Course for Free!

Email Validation Using JavaScript – Tutorial

/ / JavaScript, Latest

Email Validation Using JavaScript: Video Lesson

            This video lesson, titled “Email Validation,” shows you how to perform email validation using JavaScript. This video lesson is from our introductory JavaScript course, titled “Mastering Introductory JavaScript Made Easy v.1.0.”

Email Validation Using JavaScript: Overview

            You can perform email validation using JavaScript to validate an email address in a form field. The check is done to ensure the proper syntax and structure of an email address has been entered by the user. The code tests for the “at” symbol (@) and ensures that there is a dot (or period) near the end of the term, after the “at” symbol and at least two characters before the end of the term.

Example: <script>function validateForm()

{

var x=document.forms[“testForm”][“email”].value;

var atpos=x.indexOf(“@”);

var dotpos=x.lastIndexOf(“.”);

if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)

{

alert(“Please enter a valid email address.”);

return false;

}

}

</script>

</head>

<body>

<form name=“testForm” action=“test_form.asp” onsubmit=”return validateForm();” method=”post”>

Email: <input type=”text” name=”email“>

<input type=”submit” value=”Submit”>

</form>

</body>

Result: The code above is used to perform email validation using JavaScript and relates to the “email” section of your HTML form. The code indicates that if there is no “at” symbol (atpos<1) or the dot or period is before or too close to the “at” symbol (dotpos<atpos+2) or the dot or period is not at least two characters before the end of the email address (dotpos+2>=x.length), an error message will pop-up and alert the user to “Please enter a valid email address.” The user will NOT be able to submit the form until a valid email address has been entered.

Email Validation Using JavaScript - Tutorial: A picture of the introductory text from the "Email Validation" video lesson.

Email Validation Using JavaScript – Tutorial: A picture of the introductory text from the “Email Validation” video lesson.
TOP