Please!!! Can you help me with AJAX?

hello!

Well i have a problem with a form that i'm trying to validate it using

Ajax. Below There are my code and the problem description respectivelly:

my jsp file:

<input type="text" name="signinEmail onkeyup="validateEmail()"/>

<input type="text" id="age" name="signinAge" onkeyup="validateAge()"/>

<div id="emailMsg" ></div>

<div id="ageMsg"></div>

my javascript code:

var req;

var target;

var isIE;

function initRequest(){

if (window.XMLHttpRequest){

req =new XMLHttpRequest();

}elseif (window.ActiveXObject){

isIE =true;

req =new ActiveXObject("Microsoft.XMLHTTP");

}

}

function validateEmail(){

if (!target) target = document.getElementById("email");

var url ="emailvalidator";

initRequest();

req.onreadystatechange = processRequest;

req.open("POST", url,true);

req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

req.send("signinEmail=" + escape(target.value));

}

function validateAge(){

if (!target) target = document.getElementById("age");

var url ="agevalidator?signinAge="+escape(target.value);

initRequest();

req.onreadystatechange = processRequest;

req.open("GET", url,true);

req.send(null);

}

function processRequest(){

if (req.readyState == 4){

if (req.status == 200){

var root = req.responseXML.documentElement;

var typeTag = root.getElementsByTagName("typeTag")[0].firstChild.data

var valueTag = root.getElementsByTagName("valueTag")[0].firstChild.data

document.getElementById("msg").value = valueTag+typeTag;

if(typeTag =="email"){

setEmailMessageUsingDOM(valueTag);

}

if(typeTag =="age"){

setAgeMessageUsingDOM(valueTag);

}

}

}

}

function setMessageUsingInline(message){

mdiv = document.getElementById("userIdMessage");

if (message =="false"){

mdiv.innerHTML ="<div style=\"color:red\">Invalid User Id</div>";

}else{

mdiv.innerHTML ="<div style=\"color:green\">Valid User Id</div>";

}

}

function setEmailMessageUsingDOM(message){

var messageElement;

var messageText;

messageElement = document.getElementById("emailMsg");

if (message =="existingEmail"){

messageText ="Email j?existente!";

}

else{

if(message =="emailFormatInvalid"){

messageText ="Email inv醠ido!";

}

else{

messageText ="";

}

}

var messageBody = document.createTextNode(messageText);

if (messageElement.childNodes[0]){

messageElement.replaceChild(messageBody, messageElement.childNodes[0]);

}else{

messageElement.appendChild(messageBody);

}

}

function setAgeMessageUsingDOM(message){

var messageElement;

var messageText;

messageElement = document.getElementById("ageMsg");

if (message =="ageFormatInvalid"){

messageText ="Idade inv醠ida!";

}

else{

messageText ="";

}

var messageBody = document.createTextNode(messageText);

if (messageElement.childNodes[0]){

messageElement.replaceChild(messageBody, messageElement.childNodes[0]);

}else{

messageElement.appendChild(messageBody);

}

}

my email validation servlet:

publicclass EmailValidatorextends HttpServlet{

WebRadioDB cadastrosDB;

ArrayList list;

publicvoid init()throws ServletException{

try{

list = (ArrayList)new ArrayList();

cadastrosDB = (WebRadioDB)getServletContext().getAttribute("db");

Iterator i = cadastrosDB.getCadastros().iterator();

while(i.hasNext()){

Cadastroswebradio c = (Cadastroswebradio)i.next();

list.add(c.getEmail());

}

}catch(Exception e){

System.out.println(e.getMessage());

}

}

my age validation servlet:

protectedvoid processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

boolean flag =false;

char age[] = request.getParameter("signinAge").trim().toCharArray();

for(int i=0; i<age.length; i++){

if(!Character.isDigit(age[i])){

flag =true;

}

}

if(flag==true){

StringBuffer sb =new StringBuffer();

response.setContentType("text/xml");

response.setHeader("Cache-Control","no-cache");

sb.append("><typeTag>age</typeTag>");

sb.append("<valueTag>ageFormatInvalid</valueTag>");

response.getWriter().write("<validation>"+sb.toString()+"</validation>");

}else{

StringBuffer sb =new StringBuffer();

response.setContentType("text/xml");

response.setHeader("Cache-Control","no-cache");

sb.append("<typeTag>age</typeTag>");

sb.append("<valueTag>true</valueTag>");

response.getWriter().write("<validation>"+sb.toString()+"</validation>");

}

}

Now i'll describe step by step my problem:

step 1 load the page;

step 2 start to fill the age text field;

step 3 it validates normally;

step 4 start to fill the email text field

step 5 it doesn't validate the email textfield properlly it brings me

just the emailFormatInavlid message from the email servlet validator

and that't the problem.

the same ocurrs when i start to fiil in the email textfield

step 1 load the page;

step 2 start to fill the email text field;

step 3 it validates normally;

step 4 start to fill the age text field

step 5 it doesn't validate the age textfield properlly it brings me

just the ageFormatInavlid message from the email servlet validator

even if i fill in with a proper age format.

Thanks once more for the atention.

[10970 byte] By [Sonecaa] at [2007-10-3 2:35:13]
# 1

I haven tried yours b4 and if my suggestion is not suite for you, please dont blame me.

First, why are you using ajax is to retrieve the backend data, if not there is no use of ajax and you can use pure javascript to do it.

Second, if you want to use of ajax, you can use DWR engine, it will pretty much simple and your code, at least for me it is worked. You can download the tutorial from http://getahead.ltd.uk/dwr/documentation

ViQuEnYeEa at 2007-7-14 19:34:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

from code it all looks ok to me . Just have one println statement inside processRequest of each of validator servlet . See you are setting the flag to false for each request process and its local to service method ,so there shouldn't be any problem with multiple requests coming frequently due to keydown events through XMLHttp. And "Invalidate format" messgae only comes if your code sets flag=true explicitly insdie condition. I haven't gone through the entire cod e thoroughly , but it looks ok . Have debug statments and also you need to have some sort of queueing mechanism for processing your responses as ajax responses of keydown of age field and keydown of e-mail field may clash as there is only one cleint processing multiple repsonses.

balaji31a at 2007-7-14 19:34:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...