[an error occurred while processing this directive]

How to use email validation check through java script in jsp page

                         

This is detailed java code that explains how to use java script validator in jsp page. This validator checks for valid email given by the user.  valid_email.jsp page has 'onSubmit' attribute, which is set to ValidateForm(), in the html 'form' element. When user submits the form then this method is called which validates the email entered by the user in the text box on the page. The full code for the jsp can be seen below. Before running this code create a new directory named "user" in the tomcat-6.0.16/webapps and WEB-INF directory in same directory.

 

 

 

 

valid_email.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>check valid email</TITLE>
</HEAD>
<BODY bgcolor="#E0F8F7">
<script language = "Javascript">
/* This is code to check valid email using java script. */
function emailCheck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
    
	    // check if '@' is at the first position or 
            at last position or absent in given email 
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 ||
                str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}
        // check if '.' is at the first position or at last 
        position or absent in given email
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 ||
                str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}
        // check if '@' is used more than one times in given email
		if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
   
         // check for the position of '.'
		 if (str.substring(lat-1,lat)==dot || 
                 str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }
         // check if '.' is present after two characters 
         from location of '@'
		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 // check for blank spaces in given email
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
 		 return true					
	}
function ValidateForm(){
	var emailID=document.form.txtEmail
	
       //check if email is not entered or only spaces 
       are entereds in appropriate textbox
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		
		// set cursor to the textbox provided for email entry
		emailID.focus()
		return false
    }
	if (emailCheck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
</script>
	<form name="form" method="post" onSubmit="return ValidateForm()">
      <p>Enter an Email Address <input type="text" name="txtEmail"></p>
      <p> <input type="submit" name="Submit" value="Submit"></p>
    </form>
</BODY>
</HTML>

Save this code as a .jsp file named "valid_email.jsp" in the directory Tomcat-6.0.16/webapps/user/ and you can run this jsp page with url  http://localhost:8080/user/valid_email.jsp in address bar of the browser.


If user leave this textbox blank and click on submit button, a alert message will open.....

If user enters an invalid email id and click on submit button,  a alert message will open.....

Download source code

                         

[an error occurred while processing this directive]