Email validation is JSP using JavaScript
I this example we will show you how to validate email address in you JSP program using JavaScript.
In most of the application there is a need to validate email address on the form. In your JSP program you can use JavaScript to validate the email address. This is also a good idea to validate the form before submitting the data to the server side program.
In this example we have developed a jsp page "EmailValidation.jsp" in which onSubmit="return ValidateEmail()" of form performs the validation.
1. First we will check that input field is not.
2. Then we will do the email validation using the function emailcheck(). The funcation emailcheck() is used to verify that the input given value is a possible valid email address. This function makes sure the email address has one "@", atleast one ".". It also makes sure that there are no spaces, extra '@'s or any "." just before or after the @. It also makes sure that there is atleast one "." after the @.
In emailcheck() it is also checked that "@" must not to be at first place before any string and "." must not be at the first place.
Here is the "EmailValidation.jsp" code:
| <%@ page language="java" %> <html> <head><title>Email Validation</title> <script language = "Javascript"> function emailcheck(str) { var at="@" var dot="." var lat=str.indexOf(at) var lstr=str.length var ldot=str.indexOf(dot) if (str.indexOf(at)==-1){ alert("Invalid E-mail ID") return false } if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ alert("Invalid E-mail ID") return false } if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ alert("Invalid E-mail ID") return false } if (str.indexOf(at,(lat+1))!=-1){ alert("Invalid E-mail ID") return false } if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ alert("Invalid E-mail ID") return false } if (str.indexOf(dot,(lat+2))==-1){ alert("Invalid E-mail ID") return false } if (str.indexOf(" ")!=-1){ alert("Invalid E-mail ID") return false } alert("valid E-mail ID") return true } function ValidateEmail(){ var emailID=document.frm.txtEmail if ((emailID.value==null)||(emailID.value=="")){ alert("Please Enter your Email Address") emailID.focus() return false } if (emailcheck(emailID.value)==false){ emailID.value="" emailID.focus() return false } return true } </script> </head> <body> <form name="frm" method="post" action="#" onSubmit="return ValidateEmail()"> <p>Enter an Email Address : <input type="text" name="txtEmail"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> </body> </html> |
Output:


When proper email is input then this will show a message through alert() function as below

Tutorials
- Java 26 (JDK 26) Features with examples
- Top 10 Reasons to Learn Java in 2026
- JDK 26 Tutorial
- Java Certification Roadmap 2026: Which Oracle Certs Matter
- Install JDK 26: A Complete Beginner's Guide (2026)
- What Is Java? A Complete, Up-to-Date Guide for 2026
- Java 26 HTTP3 tutorial - HTTP 3 support in JDK 26 - How to use HTTP/3 in Java 26 application?
- JDK 28 Preview: Value Objects, Shenandoah GC & JSON API
- Java Tutorials


