Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

onclick event(alert)

 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I have one longin page(Login.html)
Here I am validating for entry in LID and PW field.
After entering login id and PW it is going to Login.java Servlet
But problem is
When user doesn't enter LID or PW ,I am showing alert and set ing focus to that filed.
if( document.forms[0].LId.value =="" &&
document.for[0].PW.value =="" )
{
alert( "you must enter valid Id and Password " )
document.forms[0].LId.focus()
}
this code is on validate function.
and clling this as
<INPUT TYPE="SUBMIT" VALUE="Login" onclick = 'validate( this )'>
it woking fine but when I add
<FORM ACTION="http://localhost:8100/servlet/Login" METHOD="POST">
After alert it directly goes to Servlet
What I have do???
padmasshree
 
bredan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First your code should be:
if( document.forms[0].LId.value =="" &&
document.for[0].PW.value =="" )
{
alert( "you must enter valid Id and Password " )
document.forms[0].LId.focus()
}
else
document.forms[0].submit();
second, add link to the button (better use image) :
<a href="javascript:validate()"><img src="*.gif"></a>
You can't use submit type.
 
Satish Kasala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi padmashree,
You have to return false when LID or PWD are blank in your javascript and also modify the onclick method call to return the boolean value of the validation. Modify the code as given below and it should work.
if( document.forms[0].LId.value =="" &&
document.for[0].PW.value =="" )
{
alert( "you must enter valid Id and Password " )
document.forms[0].LId.focus()
return false;
}
else
{
return true;
}
<INPUT TYPE="SUBMIT" VALUE="Login" onclick = 'return validate( this )'>
Satish
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic