JSP AJAX form Validation
I am sending two form field values to this jsp page and i need to insert that values to MySQL DB Table.
form fields.
Name
Email
before insert these two thing i need to check whether those fields are blank or not.
Again i need to check whether email address already taken or not.
All the Error messages should display as a in this page and then i can pass it back to HTML page using responseText;
I can check the email id from the table and here after i can't find out a way to do next validations. please help.
<%@ page language="java" import="java.sql.*" %>
<%@ include file="../config/conn.jsp" %>
<%
String driver = mysql_driver;
Class.forName(driver).newInstance();
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
int count;
String db_email=null;
String name=request.getParameter("c_name");
String email=request.getParameter("c_email");
try
{
String url=mysql_con_string;
con=DriverManager.getConnection(url);
stmt=con.createStatement();
}
catch(Exception e)
{
out.println(e.getMessage());
}
if(request.getParameter("c_submit") !=null)
{
rst=stmt.executeQuery("select c_email from clients where c_email ='"+email+"'");
while(rst.next())
{
db_email = rst.getString("c_email");
if (email.equals(db_email))
{
out.println("<font face=\"verdana\" size=\"2\" color=\"red\"><b>Already Taken</b></font>");
break;
}
}
//Inserting Records
if(db_email==null)
{
count = stmt.executeUpdate("insert into clients(c_name,c_email) values('"+name+"','"+email+"')");
if(count == 1)
{
rst=stmt.executeQuery("select c_name from clients where c_email ='"+email+"'");
while(rst.next())
{
out.println("<B>"+rst.getString("c_name")+"</B>"+" Client Registerd:::");
}
}
}
}
else
{
out.println("<font face=\"verdana\" size=\"2\" color=\"red\"><b> UnAuthorized Access !</b></font>");
}
%>
[3794 byte] By [
Ajaxranda] at [2007-11-27 0:13:52]

# 1
I think you can have one more AJAX call (call within the current one) and a ActionClass to do validation. Pass the values in this call and validate in your BO or ActionForm against the Database. The responsetext should be a boolean value.
May be I understood incorrectly. Please let me know.
skp71a at 2007-7-11 21:58:13 >

# 2
Forget about the Ajax Response Text. Only thing i need actually i need to build up a Array of Errors for blank fields and and another one for check the email id already taken or not.
the Error List should display Like this order
Name Blank:::Email Blank:::
Name blank:::email already taken:::
then i can parse the response text ibject from JS side.
please let me know how to build up the error array for the above coding without
like this.
Name blank:::null
# 3
I think you need to do it at the client side using javascript to populate an string array field in your actionform. For js array help, check out online. Thanks.
skp71a at 2007-7-11 21:58:13 >

# 4
I need to do the validation using JSP and errors should pass back to the client side. in that case i need to create a Array of Errors.
Something like this
Error1:::Error2:::
then using httpresponse text i get this Error Array to the client side and i have to parse the string to make it as bulleted list.Thats why i am using this :::
If there is way to simply display this Errors as a bulleted list from the jsp side it self no need to parse it from the client side.
i mean something like this.
* Name Cannot be left blank
* Email ID Cannot be left blank.
if it not blank again i need to check whether EMail is already taken or not.
# 5
as of me I dont see any relation to ajax paradigm, sorry. you just want JavaScript to do some plausibility checks right? so you could assign some IDs to the input elements, and add some javascript func which tells if form is ok for submission or not.
this what javascript part would look like:
<script>
function validate(){
var errDiv=document.getElementById("error_div");
var elEmail = document.getElementById("email_input");
var elName =document.getElementById("name_input");
if(elEmail==null||elEmail==""){
errDiv.innerHTML+="<li>email cannot left blank</li>";
return false;
}else if(elName==null||elName==""){
errDiv.innerHTML+="<li>name cannot left blank</li>";
return false;
}
return true;
}
</script>
well, after the JSP body (where your form starts):
(the div will store the failure messages from above)
<div id="error_div">
</div>
<form>
email: <input type="text" id="email_input"/>
name: <input type="text" id="name_input"/>
<input type="submit" onclick="return validate()" value="ok"/>
</form>
cheers
# 6
I didn't post my JS script for ajax, because this forum is for JSP. How ever only thing that i wanted to do the validation from the JSP side and generate the Errors. I am not excepting the JS script, i can do it by my end.I made JSP script also but another tiny issue. i failed to validate the user email address. I need to check whether it is in the correct format or not.
eg:
user@domain.com >>> Valid
user@domain>>>Invalid
<%@ page language="java" import="java.sql.*" %>
<%@ include file="../config/conn.jsp" %>
<%
String driver = mysql_driver;
Class.forName(driver).newInstance();
Connection con=null;
ResultSet rst=null;
Statement stmt=null;
int count;
boolean errors=true;
String name=request.getParameter("c_name");
String email=request.getParameter("c_email");
try
{
String url=mysql_con_string;
con=DriverManager.getConnection(url);
stmt=con.createStatement();
}
catch(Exception e)
{
out.println(e.getMessage());
}
if(request.getParameter("c_submit") != null)
{
if(name.length() == 0)
{
errors=false;
out.println("Name Cannot Be Left Blank:::");
}
if(email.length() == 0)
{
errors=false;
out.println("Email Cannot Be Left Blank:::");
}else
{
try
{
rst=stmt.executeQuery("select c_email from clients where c_email ='"+email+"'");
while(rst.next())
{
String db_email = rst.getString("c_email");
if (email.equals(db_email))
{
out.println("Sorry, <B>"+email+"</B> Email Already Taken:::");
errors=false;
}
}
}catch(Exception e)
{
out.println("ERROR : "+e.toString());
}
}
if(errors==true)
{
count = stmt.executeUpdate("insert into clients(c_name,c_email) values('"+name+"','"+email+"')");
if(count == 1)
{
out.println("Record Inserted Successfully:::");
}
}
}
%>
# 7
oh, I see, then I have missunderstood u,
first I would rather check for
...
if(name==null || name.length()==0){
out.println("name cannot be blank");
}
...
then to check for a valid email format, you could do some
regular expression check against it like
...
if(email==null || !email.matches("regex")){
out.println("email has invalid format");
}
...
u can find some suitable regex pattern for emails on http://www.regular-expressions.info/
that's it actually, but if you want to collect all possible errors which can occur during input data validation and then show them on one certain place, u could also create a Vector and feeding it with errors and then println() them out once