Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Form validation using jsp and AJAX

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Report post to moderator
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>
 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic