0

I have a very simple form:

<form id="formRegister">
    ....
    <input autocomplete="off" class="form-control input-sm required " id="Code" maxlength="10" name="Code" placeholder="VIP Code" style="width:150px;" type="text" value="">
    ....
    <button id="btnVIPCodeLookup" type="submit" class="button-primary">Submit<i class="icon"></i></button>
</form>

And here's the script behind it:

// Landing page form submit
$("#formRegister").submit(function (e) {
    e.preventDefault();
    var code = $('#Code').val();
    vipLookup(code);
});

function vipLookup(code) {
    showBusy();
    $.getJSON("/EventRegistration/EventRegistrationCodeLookUp", { code: code }, function (data) {
        hideBusy();
        if (data.Result == "true") {
            $('#CodeResultSpan').text('');
            $('#EncryptedCode').val(data.EncryptedVIPCode);
            ShowLegal();
        }
        else
        {
            if (data.Result == "false") {
                $('#CodeResultSpan').text('VIP Code not found. Please check the VIP Code you received and try again.');
            }
            if (data.Result == "used") {
                $('#CodeResultSpan').text('VIP Code has been used.');
            }
            if (data.Result == "error") {
                $('#CodeResultSpan').text('There was an error while looking up your VIP Code.  Please try again.');
            }
            if (data.Result == "canceled") {
                $('#CodeResultSpan').text('This VIP Code has been canceled.');
            }
            $('#Code').css('border-bottom', '1px solid #e03e3e');
        }
    });
}

The weird thing that I am seeing in some browsers (especially any iOS device, or when emulating an iOS device in Chrome), is that after the button is clicked, the form becomes disabled. I can't refocus into the textbox, and I can't click the button again.

If I remove e.preventDefault(); then the problem goes away, but this of course creates a new problem in that the page is refreshed, and the error messages vanishes.

You can see this in action here:

http://dev.walkup.audidriveusa.com/EventRegistration

Using Chrome, just type anything into the text box and click 'Submit'. You'll see the message. Then (if you can) change the text, and try to click 'Submit' again. It doesn't let you.

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • of course it stops the form, your buttons default behaviour is to submit the form.. change the button type to button and then after the code you can then submit the form with js – treyBake Jun 07 '17 at 11:35

2 Answers2

1

If your goal with the form is not to submit it with a normal request, then you should not add a submit button. Add a button with type button instead:

<button type="button" id="submitButton">some text</button>

and

$('#submitButton').on('click', function (e) {
  var code = $('#Code').val();
  vipLookup(code);
});

That will not trigger a submit of the form, then attach your eventhandler to that button instead.

If you want to handle hitting the enter key, add a handler for that.

<div id="containerToWatchForEnter">
<form>
...
</form>
</div>

and

$('#containerToWatchForEnter').on('keydown', function (e) {
  if(e.which == 13) {
    var code = $('#Code').val();
    vipLookup(code);  
  }
});

You might to only watch for hitting enter in input fields on that form - assuming you have only one form:

$('form:input').on('keydown', ...

UPDATE: You might want to watch for 'keypress' instead of 'keydown' when working with iOS.

Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
  • Ok, this is actually how I had it originally, but then the client complained that hitting 'Enter' on your keyboard did not submit the form. – Casey Crookston Jun 07 '17 at 11:42
  • You could handle that enter key as a special case. Consider where you attach your keydown handler - form or document, then check the keycode before calling your vipLookup – Per Hornshøj-Schierbeck Jun 07 '17 at 11:43
  • I really appreciate the help! Yes, only one form on this page. This does solve the problem when in desktop mode, but it's still an issue when emulating as iOS, or an an actual iOS device. As this app will be used excuslivly on an iPad, I've gotta get this figured out. oguzhancerit seems to be on to something with touchstart or touchend??? – Casey Crookston Jun 07 '17 at 11:49
  • on iphone/ipad will also send a keycode 13 when hitting the virtual keyboard enter - touchstart/end should not matter. Can you describe the problem in a different way? – Per Hornshøj-Schierbeck Jun 07 '17 at 11:53
  • yeah, I tried touchstart and it had no impact. However, I have also tried your suggestion of making the button type `button` instead of `submit`, and changing the event handler to match. It still isn't working on iOS or when emulating iOS in chrome dev tools. Both the text box and the button become locked. – Casey Crookston Jun 07 '17 at 11:56
  • Sorry - i edited my answer - keydown might not work for you if on ipad - it should work with 'keypress' instead – Per Hornshøj-Schierbeck Jun 07 '17 at 11:57
  • ok, but the 'Enter' key aside, after clicking the button the first time, I can no longer edit the text box or re-click the button (only on iOS or iOS emulator) – Casey Crookston Jun 07 '17 at 11:58
  • @CaseyCrookston I solved your problem just with CSS. When your error was printed to the screen, its parent div overlapping to the your form. Inspect on chrome after toggle the device toolbar. – oguzhancerit Jun 07 '17 at 12:02
  • @CaseyCrookston the locking of the form seems like an unrelated issue – Per Hornshøj-Schierbeck Jun 07 '17 at 12:04
  • @CaseyCrookston if it is worked, i will change my solution with the CSS solution? – oguzhancerit Jun 07 '17 at 12:04
  • sorry for the delay.... I had to take my daughter to school. I'll test now. – Casey Crookston Jun 07 '17 at 13:02
0

Your problem is; your error div that after getting the ajax request overlaps the form in mobile browsers as you see in image below.

enter image description here

You can use;

margin-top:20px;

for your error div. After solved that, just use the

return false;

instead of

e.preventDefault();

beacuse Mobile browsers don't have a click event. You need to use touchstart or touchend.

Full Code

$("#formRegister").submit(function (e) {
    var code = $('#Code').val();
    vipLookup(code);
    return false;
});

Also there are a lot of question about the e.preventDefault on mobile browsers in SO.

Mobile Safari preventDefault() not working? Android works fine

e.PreventDefault() & e.stopPropogation() not working on tablet & mobile

oguzhancerit
  • 1,436
  • 1
  • 16
  • 27
  • 1
    explain why instead of just providing a solution – treyBake Jun 07 '17 at 11:40
  • Ok, I tried this. It does seem to solve the problem on desktop browsers. But not for iOS, and not when emulating iOS in Chrome Dev Tools. As this site will be used exclusively on an iPad, this isn't going to work. – Casey Crookston Jun 07 '17 at 11:40
  • @ThisGuyHasTwoThumbs Mobile browsers don't have a click event. You need to use touchstart or touchend. – oguzhancerit Jun 07 '17 at 11:44
  • @oguzhancerit then how comes `.on('click')` works fine on mob? – treyBake Jun 07 '17 at 11:49
  • The reason why this does not work properly is you need to place the return false as the last statement - or your code will stop - and if you have code that takes a while to execute before the return statement, then the browser might decide to timeout before the return statement - leaving it up to the browser to decide what to do – Per Hornshøj-Schierbeck Jun 07 '17 at 11:50
  • @PerHornshøj-Schierbeck Should I move the "return false;" to end of the vipLookup() function? – oguzhancerit Jun 07 '17 at 11:52
  • @oguzhancerit - i would leave it in bottom of the handler - just like you have it now. I would just try to solve it differently as the practice of returning a false from an eventhandler is kinda deprecated... – Per Hornshøj-Schierbeck Jun 07 '17 at 11:58
  • I have tried using `touchstart`. Same results. The entire form is still frozen after the first button click. – Casey Crookston Jun 07 '17 at 11:59