Home Javascript Email validation is JSP using JavaScript



Email validation is JSP using JavaScript
Posted on: May 25, 2009 at 12:00 AM
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

Related Tags for Email validation is JSP using JavaScript:
javajavascriptcemailjspormdateformapplicationscriptiomailipidaddressaiappjsvalidateforaddprogramtorameilcanliuseinrmasmoscaddosjadesvalidemproppcatsspeeessatisidavascrssriripthavstatiapicaicaplprmostonogro


More Tutorials from this section

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.