• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

JavaScript in JSP

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends,
Consider the following code:


<html>
<head>
<script language="javascript" >
function log2()
{
if(document.login2.cid.value=="")
{
alert("please enter your CID");
return false;
}

}
</script>
</head>

<body bgcolor="#6495ed" text="#000000">
<h1 align="center">Welcome</h1>
<form name="login2" method="POST" onSubmit="log2()"
action="login2.do" >
<br><br>
<center>CID : <input type="text" name="cid" size ="50"></center><br><br>
<center>Password : <input type="password" name ="pwd" size="30"></center><br><br>
<center>
<input type="submit" name="submit" value="Go" >
</center>
</form>

</body>
</html>

Here login2.do maps to a servlet Login2.java which checks for the validity of the user using Customer ID and password.
I�ve included javascript which alerts the user to enter the CID ,if the user clicks submit button without entering the CID.
But when I actually run the code without entering the CID value and then clicking the submit button ,an alert message is displayed and when I click on OK button of the alert message the request is sent to the servlet (Whereas I want the same page to be displayed so that the user is given another chance to enter CID and pwd)

Cheers,
Poonam.
 
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a JSP issue. Moved to the Javascript forum.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could try this:
<form name="login2" method="POST" onSubmit="log2(); return false;" action="login2.do" >
Good luck.
 
Poonam Kadu
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pablo,
It worked.Thanks a lot.But can you please explain me why did your code work.
cheers,
Poonam
 
Bear Bibeault
Sheriff
Posts: 67645
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Poonam Kadu:

It worked.Thanks a lot.But can you please explain me why did your code work.



The syntactic sugar of placing the Javascript code in the onsubmit attribute of the form element is really just a shorthand means of doling something like:



The routine value of the onsubmit handler must be false in order to prevent form submission.

So when you used:



your handler is:



Notice that there is no return for the handler. But if you were to write:



your handler is:



Thereby returning the value of the function log2() as the return value of the handler.

Clear as mud?
[ February 20, 2007: Message edited by: Bear Bibeault ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic