JSP check email exists or not


 

JSP check email exists or not

In this tutorial, you will learn how to check from the database whether entered email-id exists or not.

In this tutorial, you will learn how to check from the database whether entered email-id exists or not.

JSP check email exists or not

In this tutorial, you will learn how to check from the database whether entered email-id exists or not. The given code accepts the email id from the user and check whether the given email id already exists or not. If it is already exist in database, then show a message 'Already exists'. Otherwise, it will show a message 'You can register now!'.

availability.jsp:

<html>
<head>
<script type="text/javascript">
function check(value){ 
xmlHttp=GetXmlHttpObject()
var url="checkajax.jsp";
url=url+"?email="+value;
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged(){ 
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
var showdata = xmlHttp.responseText; 
document.getElementById("mydiv").innerHTML= showdata;
} 
}
function GetXmlHttpObject(){
var xmlHttp=null;
try{
xmlHttp=new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<form name="form">
Email Id: <input type="text" name="email" id="email" onkeyup="check(this.value);"><font color="red"><div id="mydiv"></div></font>
</form> 

</body>
</html>

checkajax.jsp:


<%@ page import="java.sql.*" %> 
<%
String name = request.getParameter("email").toString();
System.out.println(name);
String data ="";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where email='"+email+"'");
int count=0;
while(rs.next())
{

count++;
}

if(count>0)
{
data="Email-ID already exists!";
}
else
{
data="You can register now!!!!";
}
out.println(data);
System.out.println(data);
}
catch (Exception e) {
System.out.println(e);
}
%>

Ads