How to validate email address in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To validate email address in JavaScript, check the condition for “@” and “.” i.e. for abc@gmail.com . Let’s try the following code to validate email

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var emailID;
         function validateEmail(emailID) {
            atpos = emailID.indexOf("@");
            dotpos = emailID.lastIndexOf(".");
            if (atpos < 1 || ( dotpos - atpos < 2 )) {
               document.write("Please enter correct email ID")
               document.myForm.EMail.focus() ;
               return false;
            }
            return( true );
         }
         document.write(validateEmail("abc@gmail.com"));
      </script>
   </body>
</html>
raja
Published on 30-Jan-2018 11:49:19
Advertisements