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!!
16 2176
Without code, we can only guess. Show your code or a link to a test page.
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");
-
}
-
-
}
-
-
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.
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!!
Do you see any errors when you submit?
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....
What's the error message?
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...
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?
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
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.
i changed them.. . no change in output
OK, what's the latest version of your code?
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.....
So you mean the username validation works, but the password validation doesn't?
yup .....the client side validation for password is not working...it works if i take away the ajax code
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sokolov Yura |
last post by:
Django Model is wonderfull. But SQLObject more flexible (and powerfull,
as i think, and has already more db interfaces).
But Django Model is tied with Django, and using Django with another OO...
|
by: David L Wright II |
last post by:
Does anyone know when building a msi file under VB.Net 2003, it would try to
install something from the VS 2005 Beta 1? I have both products installed.
Thanks,
|
by: Rod |
last post by:
Well, I've taken the plunge and gotten myself my first domain. (I've worked
on other peoples and of course at work, but never done my own domain.)
With this new project my family and I will...
|
by: geshan |
last post by:
In php5 I am not able to use include funciton and header("location:"""
together please help.
|
by: Bill Gower |
last post by:
I have a webapp that uses the AjaxControlToolkit. The app and ajax works
fine when run within my dev server in Visual Studio 2005 but does not work
on IIS. Any Suggestions?
Bill
|
by: buddyr |
last post by:
Hello,
I am having trouble with two combo boxes working together.
two tables table1 table2
table1 fields -number and name
table 2 fields -code and number
...
|
by: MimiMi |
last post by:
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly!
I wouldn't be surprised if my problems have something to do with...
|
by: SavRak |
last post by:
Hi,
I seem to be having an issue linking my XML document and XML Schema together.
Both are not working, and do not seem to be validating when using:
http://tools.decisionsoft.com/schemaValidate/
...
|
by: software4 |
last post by:
So I am not great at Javascript, but i have 2 scripts that i want to work with each other. When i use one by itself, they each work fine, but when i add them together it does not work.
I have a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |