Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent the Go button on iPad/iPhone from posting the form

Tags:

I have a dynamic form that is to be displayed using an iPad.

This form has a couple of radio buttons and some text fields and one submit button.

In an iPad the virtual keyboard GO button is supposed to act ad the enter key, causing the first submit button in the form to be clicked and the form to be posted.

To avoid excessive involuntary postings before the form is complete we added an extra submit button higher up in the form, absolutely positioned outside of the visible area with onclick="return false;". This hijacks the enter keystroke preventing accidental posting in every browser except Safari Mobile.

On an iPad we even tested Opera mobile and it works as expected.

But Safari Mobile apparently ignores the return false since event clicking the button causes a post that no other browser does, not even safari on PC.

My questions are

1: Why is safari mobile ignoring "return false" on submit, is there an other mechanism at play here?

2: How can I stop Safari mobile from posting the form when clicking GO?

I have made numerous searches on Google and Stackoverflow and found many examples but all requires a lot of javascript and event binding and the dynamic nature of the form along with user generated content makes this error prone and pretty complex since almost all required binding events to every textbox and textarea.

Any solution that works is good but the simpler the better, especially if it does not require to much customization of the form or events that might conflict with autocomplete or validation events.

Example testpage: http://lab.dnet.nu/ipad.php

like image 591
David Mårtensson Avatar asked Mar 08 '12 08:03

David Mårtensson


People also ask

How do I put the home button on my iPad?

You can adjust accessibility settings for the Home button (on an iPad with a Home button) or top button (on other iPad models). Go to Settings > Accessibility, then tap Home Button (on an iPad with a Home button) or Top Button (on other iPad models).


2 Answers

I found a solution to my problem.

The base to the problem is that Safari mobile ignores onsubmit="return false" on buttons, it only works on forms.

Setting onsubmit="return false;" on the form, making a normal button (not submit) and setting onclick="form.submit()".

Ex.

<form method="post" onsubmit="return false;">     ... //Other fields here      <input type="button" value="Send" onclick="form.submit();" /> </form> 

The Go button does not trigger a normal button, only submit buttons. Since the form has onsubmit="return false;" it will not post.

The button on the other hand, when clicked triggers the onclick="form.submit();" which overrides the onsubmit on the form.

This solution seems to work in any browser reliably.

like image 56
David Mårtensson Avatar answered Sep 18 '22 13:09

David Mårtensson


Better answer is this. The other does not allow you to use a regular submit button. This attacks just the go button.

$("body").keydown(function(){     if(event.keyCode == 13) {         document.activeElement.blur();         return false;     } }); 
like image 36
Tim Ho Avatar answered Sep 21 '22 13:09

Tim Ho