Email-id Validation in Javascript

 Program:

<html>
<head>
<script>
//the input data must contain an @ sign and at least one dot (.).
// Also, the @ must not be the first character of the email address,
//and the last dot must be present after the @ sign, and minimum 2 characters before the end:
function validateForm() {
    var x = document.forms["myForm"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        alert("Not a valid e-mail address");
        return false;
    }
    else {
     alert(" a valid e-mail address");
        return True;
      
}
}
</script>
</head>

<body>
<h1>Email-id validation:</h1>
<form name="myForm"  onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
</body>

</html>

Output:


Email-id validation:

Email:

No comments:

Post a Comment