File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Ajax jsp form validation problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Ajax jsp form validation problem?" Watch "Ajax jsp form validation problem?" New topic

Ajax jsp form validation problem?

Manas Saxena
Ranch Hand

Joined: Mar 01, 2013
Posts: 38
I have made a registration form and i am validating email id and password using ajax.
The ajax is working fine and appropriate message is displayed for wrong entry.Heres the email id sample code:

Email id<input type="text" size="34" name="email_id" id="email_id" onkeyup="emailval(email_id.value)">

the ajax function emailval calls a jsp page known as regajax which then checks required conditions and prints apt message.

The problem is how will my form know that the entries are wrong since my ajax call simply prints the apt message.Is it possible to return some boolean value from an ajax call which i could pass on to the form.If yes then how do i pass the value returned by ajax method to the form so that if entries are wrong then the form should not be submitted.
If no then what should be my approach.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63867

Manas Saxena wrote: since my ajax call simply prints the apt message.

That's your problem. You're just returning semantic-less text and shoving it into the DOM. Not a good approach,

Rather, submit the Ajax to a servlet (never, ever, ever, ever use a JSP for processing) that returns JSON data (which could be as simple as true or false, or as complex as you like). JavaScript can then set an error message, set a class on the form field that indicates it is error, or do anything else that needs doing.

Relying on JSP for this is a grave mistake.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Ajax jsp form validation problem?
It's not a secret anymore!