admin管理员组

文章数量:1326478

HI All

I have following lines in my JSP.

<s:submit name="submit"  onclick="return validateUser();" action="saveUser"  theme="simple" value="Save" />

The java script method validateUser(), validates the user and returns true or false. The form should not be submitted when the validation fails.

This is working in the FF but not in the IE8.

IE8 submits the form even after validation fails.

HI All

I have following lines in my JSP.

<s:submit name="submit"  onclick="return validateUser();" action="saveUser"  theme="simple" value="Save" />

The java script method validateUser(), validates the user and returns true or false. The form should not be submitted when the validation fails.

This is working in the FF but not in the IE8.

IE8 submits the form even after validation fails.

Share Improve this question asked Mar 8, 2011 at 6:54 ajmajm 13.2k59 gold badges169 silver badges241 bronze badges 9
  • does IE give an error message? – JohnP Commented Mar 8, 2011 at 6:56
  • 1 Try this onclick="javascript:void(0);return validateUser();" – Furqan Hameedi Commented Mar 8, 2011 at 6:57
  • No, IE does not give any error message. The form gets submitted properly. – ajm Commented Mar 8, 2011 at 6:57
  • 2 @Ashish Instead of using onclick try attaching your javascript to onsubmit. That is, attach your function to the form tag, using onsubmit. – matthewpavkov Commented Mar 8, 2011 at 6:58
  • @matthewpavkov Basically I have a single form and many submit buttons in same form. There are different action needs to be performed on click of each submit button. Hence, I have given onclick event on each submit button. – ajm Commented Mar 8, 2011 at 7:01
 |  Show 4 more ments

4 Answers 4

Reset to default 3

Never assume you can cancel the submit button, instead set some javascript variable or hidden field on the form and use onsubmit. Take my word for it. Have the onsubmit look at the variable set by the different submit buttons

Never use javascript: (javascript colon) unless you are in IE and have a VBScript as the first script on the page. In all other cases javascript is default.

Never use such atrocities as <a href="javascript:something()" instead of <a href="#" onclick="return something()

Lastly, in IE, when you have an error occurring, the default action is to submit the form. You may very well have other errors pletely elsewhere and have the validate return the error, which is seen as true (0 evaluates to false, most anything else is true)

<script type="text/javascript">
var isvalidateNeeded = true;
function validate(theForm) {
  if (!isvalidateNeeded) return true; // allow submission
. // normal validation
.
.
  return true; // allow submission
}
</script>
<form onsubmit="return validate(this)">
.
.
.
<input type="submit" name="subaction" value="Test" onclick="isvalidateNeeded=false" />
<input type="submit" name="subaction" value="Check" onclick="isvalidateNeeded=false" />
<input type="submit" name="subaction" value="Submit" onclick="isvalidateNeeded=true" />
</form>

First, a single form that can perform multiple actions is a bad idea, that said...

Do not use submit buttons, instead:

<button type="button" onclick="javascript:validateUser();">Save user</button>
<button type="button" onclick="javascript:deleteUser();">Delete user</button>

Now you only have to worry about default submit behaviour of a form (when user presses enter in a field).

Only thing you need to change is in your validateUser() function. IE is looking for the return value on the event, so you need to specify this:

event.returnValue = true;
return true;

event.returnValue = false;
return false;

Please note that if there is a bug or error in validateUser() or deleteUser() then "return false" will NOT stop the anchor action and your browser will try to link to the href "subaction". Here is the logic:

  1. User clicks on anchor
  2. onClick fires validateUser()
  3. There is an error in validateUser() so Javascript crashes and stops all code execution.
  4. return false is never fired because Javascript has already stopped.
  5. browser tries to go to href attribute

If you are relying on a third party JavaScript API (I was using code supplied by Recyclebank to spawn a popup), and the third party API makes an update that breaks the JavaScript, then you'll have problems.

The following will stop the link under normal conditions and error conditions.

<a class="button" href="javascript:;" onclick="validateUser();return false;">Validate User</a>

本文标签: jspJavascript return false not working in ieStack Overflow