Development| HTML| JavaScript| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Email validation is JSP using JavaScript 
 

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

                         

» View all related tutorials
Related Tags: java javascript c attributes script reference object method sed remove ip attribute node move nat to ref e it alter

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

a login page with user name and pasword should get as input from user in a servlet using post method and it should be validated and post the resul to the jsp
the user name and password should not be visible in the address bar for others

Posted by ANAND on Friday, 09.26.08 @ 16:48pm | #80721

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.