I am developing a simple web application. It doesn't have any database connections. Just simple page flows. In all the pages, I am calling a javascript function which validates for the empty or null values of the form fields, and then submits if everything is proper. The problem I am facing is, all the pages which contain a single textbox in the form, gets submitted before the javascript function is processed. Though the javascript function gets called on click of the button, the page gets submitted in the background. But this does not happen in other pages which contain more than one textbox though the function and logic is the same everywhere. Can anyone tell me what could be the problem.
We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.
And since this really has nothing to do with JSP, I'm moving it along to the HTML/Javascript forum.
Ramaswamy Srinivasan
Ranch Hand
Joined: Aug 31, 2004
Posts: 295
posted
0
Hi Tiger,
Can you tell, where are you actually submiting the form? Are u doing it by specifying the form action="/servlet/or something" in the form or submitting it explicitly using the Javascript?
Also check this...are the buttons are the type submit or button?
If it is submit, try changing it to button and then try submitting the form.
Cheers, Swamy
Naani Mui
Greenhorn
Joined: Oct 03, 2004
Posts: 12
posted
0
I am submitting the form from the javascript function. Only one function in my javascript file contains the submit function call.
Also I am not using any buttons in the form. It contains an image.
WHy I find it strange is, the page works fine on click of the image - it calls the validation functions and if everything is proper, then submits the page. But when I press enter, the page gets submitted without calling any of the javascript functions. I am capturing the key events on the document and if the key pressed is "Enter" I am calling the validation function. The validation function returns true if the form field is NOT NULL. Returns false if it is empty. If true is returned, the page should get submitted. If false is returned, the page should NOT get submitted and it should return focus to the respective form field. This is not happening on enter press.
The same page works fine when I click on the image for submitting the page.
When I press "Enter" though the validation function is called, the page gets submitted in the background which I can see while the alert is displayed.
All these things happen only if the page contains a single text box. If it has more than one textbox, everything works fine - ALL THE FUNCTIONS AND LOGIC IS THE SAME IN ALL PAGES - ALL THE JAVASCRIPT FUNCTIONS ARE IN A SINGLE JS FILE.
Please help..
Balaji Loganathan
author and deputy
Bartender
Joined: Jul 13, 2001
Posts: 3150
posted
0
Hi Java Rani, Like Bear Bibeault said please change your display name.
What is the 'name' of your submit button? You didn't call it 'submit', did you? If you did, try making sure no elements are called 'submit', rename them 'btnSubmit' or something.
A href="javascript:fnFeedBack('x','y','z')" img src="images/update.gif" ALIGN=TOP width="97" height="26" border="0" /A
script Language="JavaScript" var thisForm = document.DummyForm; isFullyLoaded = true;
var tb1 = thisForm.tb1;
function checkForm() { var fieldHandle = thisForm.tb1; var fieldValue = fieldHandle.value; if (!isInteger(fieldValue)) { //return sendErrorMessage(fieldHandle, getisIntegerErrorMessage()); } if (isEmpty(tbQuestion_1.value) || (tbQuestion_1.value < 0)) { alert("Please enter a whole number of 0 or greater."); getFocus(tbQuestion_1); return false; } return true; } function isEmpty(s) { for (i = 0; i < s.length; i++) { if (s.charAt(i) != " ") return false; } return true; } function isInteger(s) {
if (isNaN(s) || isNaN(parseInt(s)) || (parseInt(s) != parseFloat(s)) || (s.indexOf(".") >= 0) ) { if (!isEmpty(trim(s))) { return false; } else { return true; } } else { return true; } } function fnFeedBack(a,b,c) { if(checkForm()) { fnSubmit(a,b,c); } } function fnSubmit(aa,bb,cc) { thisForm.submit(); } var isNav, isIE; document.keyup = checkChar; function checkChar(evt) { var Key; if (isNav) { Key = evt.which; } else { Key = window.event.keyCode; }
Not sure, if this is the issue, but: Since the form contains only 1 element, which is text, and you dont have any handler for onsubmit in a form tag, form will get submitted, if user hits "Enter". You should handle keydown opose to keyup. [ October 05, 2004: Message edited by: Yuriy Fuksenko ]
Naani Mui
Greenhorn
Joined: Oct 03, 2004
Posts: 12
posted
0
THANKS A LOT. I WAS ABLE TO FIX THE PROBLEM BY ADDING SOME JAVASCRIPT IN THE FORM ONSUBMIT EVENT.
THANKS ONCE AGAIN.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.