ajax and js not working together!! | Newbie | | Join Date: Sep 2008
Posts: 10
| | |
hi,
am very new to ajax and js . i tried using ajax for server validation in my jsp. i also used js for client side validation
i have my js script for client side validation in a seperate file. i embedded the ajax code in the jsp itself.
the client side validation and the ajax code are working when i use them seperately.
but when i try to use them both , the client side validation is not taking place at all. my page is directly dispatched to the validation servlet in the server .
is it a problem to include two seperate js files in a single jsp file?
pls help me out!!
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
Without code, we can only guess. Show your code or a link to a test page.
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
this is the jsp page i tried
the script file i tried is this fun.js - function validateFormOnSubmit(theForm) {
-
var reason = "";
-
reason += validateUsername(theForm.id);
-
reason += validatePassword(theForm.Password,theForm.Password2);
-
-
-
if (reason != "") {
-
alert("" + reason);
-
return false;
-
}
-
-
return true;
-
}
-
-
-
function validateUsername(fld) {
-
var error = "";
-
var illegalChars = /\W/; // allow letters, numbers, and underscores
-
-
if (fld.value == "") {
-
fld.style.background = 'Yellow';
-
error = "Enter a Username !!.\n";
-
} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
-
fld.style.background = 'Yellow';
-
error = "The username should be of atleast 5 characters.\n";
-
} else if (illegalChars.test(fld.value)) {
-
fld.style.background = 'Yellow';
-
error = "The username contains illegal characters.\n";
-
} else {
-
fld.style.background = 'White';
-
}
-
return error;
-
}
-
-
-
function validatePassword(fld2,fld2) {
-
var error = "";
-
var illegalChars = /[\W_]/; // allow only letters and numbers
-
-
if (fld1.value == "") {
-
fld1.style.background = 'Yellow';
-
error = "Pls Enter a Password.\n";
-
}else if(fld2.value == "") {
-
fld2.style.background = 'Yellow';
-
error = "Pls Enter the Password again.\n";
-
}else if ((fld1.value.length < 6) || (fld1.value.length > 15)) {
-
error = "The password should be atleast 6 chars \n";
-
fld1.style.background = 'Yellow';
-
}else if (illegalChars.test(fld1.value)) {
-
error = "The password contains illegal characters.\n";
-
fld1.style.background = 'Yellow';
-
} else if(fld1.value!=fld2.value)
-
error="The password in the two fields must match\n"
-
fld2.style.background="Yellow";
-
else {
-
fld.style.background = 'White';
-
}
-
return error;
-
}
this is the servlet - protected void doGet(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
-
ResultSet rs;
-
String sql;
-
-
request.setCharacterEncoding("UTF-8");
-
-
-
Statement stmt ;
-
String name=request.getParameter("id").toString();
-
-
-
-
-
-
try {
-
//loading the driver
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
-
//connection object created using DriverManager class
-
//student_base is the name of the database
-
Connection connect =DriverManager.getConnection("jdbc:odbc:student_base");
-
-
-
-
-
stmt=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
-
-
//creating prepared statement object pstm so that query can be sent to database
-
-
-
-
-
//execute method to execute the query
-
rs= stmt.executeQuery("SELECT PASSWORD FROM STUDENT_BASE"+" WHERE NAMES = '" + name + "'");
-
-
RequestDispatcher rd;
-
if(rs.next()) //username exists
-
{
-
response.setContentType("text/xml");
-
response.setHeader("Cache-Control", "no-cache");
-
response.getWriter().write("<valid>false</valid>");
-
-
//forwarding to the corresponding view
-
}
-
-
else //code when username does not exist
-
{
-
response.setContentType("text/xml");
-
response.setHeader("Cache-Control", "no-cache");
-
response.getWriter().write("<valid>true</valid>");
-
-
-
}
-
-
-
rs.close();
-
-
connect.close();
-
-
} catch(SQLException sqe) {
-
System.out.println(sqe.getMessage());
-
} catch(ClassNotFoundException cnf) {
-
System.out.println("Class not found error");
-
}
-
-
-
}
-
-
/** Handles the HTTP <code>POST</code> method.
-
* @param request servlet request
-
* @param response servlet response
-
*/
-
protected void doPost(HttpServletRequest request, HttpServletResponse response)
-
throws ServletException, IOException {
-
-
ResultSet rs;
-
String sql;
-
-
response.setContentType("text/html;charset=UTF-8");
-
-
-
Statement stmt ;
-
String name=request.getParameter("id").toString();
-
String Password=request.getParameter("Password").toString().trim();
-
-
-
-
-
-
try {
-
//loading the driver
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
-
//connection object created using DriverManager class
-
//student_base is the name of the database
-
Connection connect =DriverManager.getConnection("jdbc:odbc:student_base");
-
-
-
-
-
stmt=connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
-
-
//creating prepared statement object pstm so that query can be sent to database
-
-
-
-
-
//execute method to execute the query
-
rs= stmt.executeQuery("SELECT PASSWORD FROM STUDENT_BASE"+" WHERE NAMES = '" + name + "'");
-
-
RequestDispatcher rd;
-
-
if(rs.next()) //username exists
-
{
-
-
//add code to take to the next page
-
-
-
}
-
-
else {
-
PreparedStatement pstm=connect.prepareStatement("insert into student_base values(?,?)");
-
pstm.setString(1,name);
-
pstm.setString(2,Password);
-
//execute method to execute the query
-
pstm.executeUpdate();
-
-
rd=request.getRequestDispatcher("success.view");
-
-
-
-
rd.forward(request,response);
-
//closing the prepared statement and connection object
-
pstm.close();
-
-
}
-
rs.close();
-
connect.close();
-
-
} catch(SQLException sqe) {
-
System.out.println(sqe.getMessage());
-
} catch(ClassNotFoundException cnf) {
-
System.out.println("Class not found error");
-
}
-
-
}
-
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
On line 73, change - <SCRIPT TYPE="TEXT/JAVASCIPT" SRC="fun.js"></SCRIPT>
to - <script type="text/javascript" src="fun.js"></script>
and see if that makes a difference.
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
nope !! i tried it!!no diff wotsoever......
pls help me out...i am stuck up with this for more than a day... iam having no bright ideas with my meagre knowledge of ajax and js.....
thanks for the effort though!!
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
Do you see any errors when you submit?
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
the page is loading with a script error.. IE show the message that the page loaded with error... mozilla is showing no errors.... ajax is working alone fine anyway..
also everytime i change the focus from the textbox under question the" page has error" is intimated in the IE... so am not sure whether its a problem because of the ajax or the js .....
i tried including the client side js functions which i now hav in fun.js to the jsp itself along with ajax part.. in that case both of the validations are not working....
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
What's the error message?
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
am talkin abt the error that is shown at the bottom most corner of the page to the left of the status bar in the browser..... it just shows page loaded with error and the alert symbol...no other indication.....my server is not throwing up any exception or anything in the server log...
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
In IE, if you double click the icon, you should see the error message and line number. Are you sure there's nothing in the Firefox console?
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
ya mozilla shows no error....the error in ie is showing an line no which is not a part of the js..it belongs to a code in form tag in jsp
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
I notice a few problems in your code. Use something else instead of id for the name because id is already a property of the form and therefore may cause it to behave erratically. Alternatively, use theForm.elements["id"] to refer to the element.
You've not closed the two password fields with the closing angle bracket >
The validatePassword() function has two fld2 as arguments.
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
i changed them.. . no change in output
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
OK, what's the latest version of your code?
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
basically i made all the changes you suggested.... i tried using the client side validation and ajax validation for the first field in the same function..both are working fine...but if i introduce another function which does client side validation for second field its not taking place.....
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: ajax and js not working together!!
So you mean the username validation works, but the password validation doesn't?
| | Newbie | | Join Date: Sep 2008
Posts: 10
| | | re: ajax and js not working together!!
yup .....the client side validation for password is not working...it works if i take away the ajax code
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|