wood burning stoves 2.0*
The moose likes JSP and the fly likes Form validation using jsp and AJAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

Form validation using jsp and AJAX

Manas Saxena
Ranch Hand

Joined: Mar 01, 2013
Posts: 38
I am trying to validate a form using ajax.My code (which i am putting below) works for email id validation however i am not able to get it work for confirming password.Please help.Heres the code

reginfo.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="template1.css">
</head>
<script type="text/javascript">
function xmlhttpPost(strURL) {
var xmlHttpReq = false;
var self = this;

// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring());

}
function getquerystring() {

var word = document.getElementById("email_id").value;
var word1 = document.getElementById("password").value;
var word2 = document.getElementById("confirm_password").value;
alert(word);
alert(word1);
alert(word2);
word = 'w='+ escape(word);
word1 = 'w1='+ escape(word1);
word2 = 'w2='+ escape(word2); // NOTE: no '?' before querystring
return word;
return word1;
return word2;


}


function updatepage(str){
document.getElementById("result").innerHTML = str;
document.getElementById("password_result").innerHTML = str;
}

</script>
<body>

<div class="header">
<div class="top_info">


</div>
<div class="logo">

<h1><a href="#" title="Centralized Internet Services"><span class="dark">SITE</span>
LOGO</a></h1>
</div>
</div>
<div class="bar">
<ul>
<li class="browse_category">Welcome to SITE NAME</li>


</ul>
</div>
<div class="reg_center">
<h3>REGISTRATION</h3><br/>
<form name="reg" method="post" >
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="10" FRAME="box">
<TR>
<TD><font face="Chiller" size="5"><b>First Name:</b>    <input type="text" size="35" name="first_name"></font></TD>

<TD><font face="Chiller" size="5">  <b>LastName:</b>      <input type="text" size="35" name="last_name"></font></TD>

</TR>
<tr></tr>
<TR>
<TD><font face="Chiller" size="5"><b>Gender:</b></font>        <input type="radio" size="35" name="gender">Male
    <font face="Chiller" size="5"><input type="radio" size="35" name="gender">Female </font>
</TR>
<TR>
<TD><font face="Chiller" size="5"><b>BirthDate:</b>     <input type="text" size="35" name="date_of_birth"></font></TD>
</TR>

<TR><TD><font face="Chiller" size="5"><b> (mm/dd/yyyy)</b></font></TD></TR>


<TR>
<TD><font face="Chiller" size="5"><b>Email id:</b>       <input type="text" size="34" name="email_id" id="email_id"><div id="result"></TD>
</TR>


<TR>
<TD><font face="Chiller" size="5"><b>Password:</b>      <input type="password" size="34" name="password" id="password"><div id="password_result"></TD>
<TD><font face="Chiller" size="5"><b>Confirm Password:</b><input type="password" size="35" name="confirm_password" id="confirm_password"></TD>
</TR>
<tr></tr>
<tr>
<td colspan="2">
<font face="Chiller" size="5"><center><input type="button" value="Register" onclick='JavaScript:xmlhttpPost("regajax.jsp")'></center>
</td>
</tr>
</table>

</form>

</div>

<div class="footer">

</div>
</div>

</body>
</html>




regajax.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
#header{
color:red;

}
</style>
</head>
<body>
<%
String w = request.getParameter("w");
String w1 = request.getParameter("w1");
String w2 = request.getParameter("w2");

String msg =w;
String ter="Invalid email id";
String ter1="Password don't match";
%>
<%=w1%>
<%=w2%>
<%
int atpos=w.indexOf("@");
int dotpos=w.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=w.length())
{%>
<%=ter%>
<%}
if (w1!=w2)
{%>
<%=ter1%>
<%} %>

</body>
</html>
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

Hi Manas,

Please use JSP Forum for questions related to JSP. This is a JDBC forum. Also, please put your code withing [code] tag. Its easy to read.


Thanks,
Tushar (SCJP 1.5)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Form validation using jsp and AJAX
 
Similar Threads
JSP,JDBC
The form is not submitting
Value in the else condition is getting printed as the page loads
404 file not found error
Value in the else condition is getting printed as the page loads