File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes javaScipt onsubmit problem in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "javaScipt onsubmit problem in jsp" Watch "javaScipt onsubmit problem in jsp" New topic
Author

javaScipt onsubmit problem in jsp

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 328

hi ranchers,

here is a jsp file which accepts the user id and password.When user does not enters the id and password and submit the form
then it should display the alert box message as wriiten in javascrpit.

but what is happening is it does not showing alert box when i submit the form without providing id and password.




can anyone help with this.


Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

Use a JavaScript debugger to see what's going in inside your function. Find out:
Is the function being called at all?
Is it fetching the right element values?
Is the comparison working as expected?

and so on

All modern browsers have JavaScript consoles except for Firefox, for which you can use the Firebug plugin.

And, why are you checking for null?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 50
Hi Naveen,

There's a very minor issue in your code. Here it is :

In your javascript function



you have defined both variables as x. whereas the second one where you collect the password should be y.

Moreover instead of using the && operator in the check, you should use ||. Because && operator will not satisfy the condition where one of the fields is null. So all in all your code should be better written as



To enhance it even further you can do separate validation for user id and password. Also note that as good coding practices, its better to give meaningful names to variables.

Hope this helps!

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

Do you not think it would have been better to help him learn how to find such errors himself?

Sigh.

In any case, I ask again. Why the comparisons for null? The value of an element will never be null.
Komal Renu
Ranch Hand

Joined: Jul 21, 2011
Posts: 50
Yes a check with null isn't actually required. my bad!

Komal
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 328

Komal Renu wrote:Hi Naveen,

There's a very minor issue in your code. Here it is :

In your javascript function



you have defined both variables as x. whereas the second one where you collect the password should be y.

Moreover instead of using the && operator in the check, you should use ||. Because && operator will not satisfy the condition where one of the fields is null. So all in all your code should be better written as



To enhance it even further you can do separate validation for user id and password. Also note that as good coding practices, its better to give meaningful names to variables.

Hope this helps!

Thanks and Regards,
-------------------------------------------------------------------------------------
Komal Renu | krenu@infocepts.com | www.infocepts.com
-------------------------------------------------------------------------------------



yes that was the problem and it is resolved.

thanks


Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
You should really validate your HTML form, it has tons of errors.

http://validator.w3.org/

Eric
 
 
subject: javaScipt onsubmit problem in jsp
 
Threads others viewed
does firefox have a problem with .setAttribute ?
why document.getElementById work with form elements
JSP not calling JS file funtion
validate form fields using javascript in struts
JS text in html page
MyEclipse, The Clear Choice