Home Javascript Email validation is JSP using JavaScript
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Email validation is JSP using JavaScript

Advertisement
I this example we will show you how to validate email address in you JSP program using JavaScript.

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

Download SourceCode

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 25, 2009

Ask Questions?    Discuss: Email validation is JSP using JavaScript   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Darryl
April 23, 2012
I don't get it

Everytime I see javascript form validators like this one they invariably leave out the details of how to actually submit the form. In your example, the action is "" and I don't see in your validation script where you actually submit the form. Would you mind clearing this up?
DMCA.com