Disable the “Go” Button on the Android and iOS Virtual Keyboard

I recently had a project where I wanted to stop the “Go” button on the Android and iOS popup keyboard from submitting a form on my web application. Why would anyone want to do this?  In my case, I wanted to prevent accidental form submission (or “butt-dialing”) on my app.  I had implemented a “swipe-to-save” feature, but the “Go” button was bypassing this and submitting the form without requiring the swipe! After a lot of research and experimentation, I believe I have a solution that works!  I’ve tested it on iOS and Android (on HTC One).  Please let me know if it doesn’t work on your device!

Android virtual keyboard Go button
Android virtual keyboard Go button

Theory

The basic idea is to use JavaScript to detect key presses in your form <input> fields. When a key press is detected, you check to see if it was the “Go” key.  If so, you take focus off of the keyboard, which hides it, then return “false” to prevent submission of the form, and voila!  Pressing “Go” will make the keyboard disappear instead of submitting the form.

The Code

Here it the code, which uses jQuery (your code might use “$” instead of “jQuery”):

jQuery(document).ready(function() {
    jQuery('input').keypress(function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if ( (code==13) || (code==10))
            {
            jQuery(this).blur();
            return false;
            }
    });
});

Make sure you are sourcing the jQuery file, then add this code to the JavaScript section of your code and you should be set! If you want some other action to happen after the keyboard disappears, you can add it right after the blur() line. Let me know if it works for you or not! – Brian

Shares

Please Leave a Question or Comment

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

13 Comments
Inline Feedbacks
View all comments
David
David
4 years ago
Anonymous
Anonymous
6 years ago

Thanks, man, used it in a real commercial project and it’s working great :)

Anonymous
Anonymous
7 years ago

Does not work. e.keyCode and e.which are always 229 for Android virtual keyboards, see http://stackoverflow.com/questions/30743490/capture-keys-typed-on-android-virtual-keyboard-using-javascript

Anonymous
Anonymous
7 years ago

Life saver man! Thks a lot!

Anonymous
Anonymous
7 years ago

Line 9, that end bracket isn’t needed…

Eduardo in Norway
7 years ago

jQuery? Come on it is 2016.

Anonymous
Anonymous
8 years ago

Very good! This is all I needed. Thanks a lot!

Vignesh Prabhu
8 years ago

HI man,
This is really helpful, But my problem hasn’t been solved yet.
I wanted to give focus to the next form element on every click of go,(Like in tabindex)
but this thing is not working in IOS 9
Will be really grateful if you can help this one out

Anonymous
Anonymous
8 years ago

Great!! It works!! thanks man!