Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

jsp - FORM with javascript and servlet not working

Status
Not open for further replies.

AlyssaK1

Programmer
Jan 28, 2007
2
Hi everyone,

I'm using a validation javascript that is called onsubmit="return validate()".

This javascript is used to validate a login form. However now I have a servlet that runs off the same FORM - to check if the user exists etc.

<form name="login_form" method="post" action="Controller" onsubmit=" return validate()">

What would the correct way be to implement the scenario where the form only calls the servlet when validate javascript has returned true.

I have found it does enter the validate javascript function however it doesn't check if the fields are empty string, which it did without the servlet action. The servlet automatically returns a jsp page stating the login details don't exist in the database. Which obviously they don't.

Within the validation function valid = true automatically unless a field has an empty string then valid= false and the form should not be submitted.

If you have any ideas a response would be great.

Thanks, Alyssa
 
The code you've shown above should call the validate function with no problems, so perhaps there's another issue:

- You might be submitting the form using JS to call the 'submit' method - which will bypass the JS handler

- You might have multiple JS functions called 'validate'

- You might have a JS error somewhere

Perhaps posting a URL might give more away?

Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Difficult without the code, but I'll make I guess: the operation for comparisons is ==, not =

Cheers,
Dian
 
Hi everyone,

Thanks for your responses. I should of posted this code earlier i thought there might have been an option or a way to somehow specify the javascript must be checked first then call the controller within the FORM definition. If the action=controller is taken out the javascript works.

There are no errors on the jsp page.

Here's some code..


The javascript code:
Code:
<script type="text/javascript">

function validate_form ( )
{
valid = true;
    			
    if ( document.login_form.username_output.value == "" )
    			{
       	alert ( "Please fill in the 'User Name' field." );
       				
       	return false;
    }
    if ( document.login_form.password_output.value == "" ){
       	alert ( "Please fill in the 'Password' field." );
       				 
       	return false;
    }
    			
    if(valid){
	alert("valid: true");
        window.open('loginstatement.jsp', 'statementwindow', 'width=850,height=600,resizable=no');
				
    }
}
		</script>
The form:

Code:
<form name="login_form" method="post" action="Controller" onsubmit="return validate_form ( )">
								<input type="hidden" name="action" value="login"/>

										<table cellSpacing="3" cellPadding="0" border="0" style="border: medium none" id="loginTable">
											<tr>
												<td class="smallheaderfont">
													<label for="login_username" style="cursor: default">User Name</label>
												</td>
												<td class="inputfieldfont">
													<input class="bginput" id="login_username" style="FONT-SIZE: 11px" onfocus="" accessKey="u" tabIndex="1" size="10" name="login">
												</td>
											</tr>
											<tr>
												<td class="smallheaderfont">
													<label for="login_password" style="cursor: default">Password</label>
												</td>
												<td class="inputfieldfont">
													<input class="bginput" id="login_password" style="FONT-SIZE: 11px" onfocus="" accessKey="u" tabIndex="2" size="10" name="password">	
												</td>
											</tr>
											<tr>
												<td></td>
												<td class="inputfieldfont">
													<input class="button" accessKey="s" tabIndex="104" type="submit" value="Log in" align= "right" onMouseover="ddrivetip('Enter your username and password to login, or click the register button to register','white', 150)";
													onMouseout="hideddrivetip()">
												</td>
											</tr>
										</table>
									</form>

I would also like another window to open with a statement that users must either accept or decline before logging in. This is the reason for the valid= true window.open portion of the code in the javascript

Thanks for your help!! Alyssa.

 
Hi

Your [tt]function[/tt] returns nothing if everything is correct. Add a [tt]return false[/tt] statement at the end, so the [tt]form[/tt] will be never submitted.

Instead submit the [tt]form[/tt] from loginstatement.jsp if the visitor accepts whatever you want :
Code:
function call_me_it_the_visitor_accepted()
{
  if (opener && !opener.closed && opener.document && opener.document.login_form && 'function'==typeof opener.document.login_form.submit) opener.document.login_form.submit()
}
Note that I only answered your question, I do not suggest to do this popup thing.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top