简体   繁体   中英

jQuery - select function is not working on mobile

I made an OTP design. So I have 6 inputs and everytime the user types a number it will go automatically to the next input. I have made the code below but the .select() is not working on mobile phones. Can someone help me with this?

Codepen: https://codepen.io/aceraven777/pen/wvmbadQ

HTML

<div class="otp-group">
    <div class="otp-digit otp-digit-1">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-2">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-3">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-4">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-5">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-6">
        <input type="text" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>
</div>

JS

$('.otp-digit').each(function() {
    var $otp_digit = $(this);
    var $input = $otp_digit.find('input');

    $input.attr('maxlength', 1);
    $input.attr('pattern', '\\d*');

    $input.on('keyup', function(e) {
        var $input = $(this);
        var $otp_digit = $input.closest('.otp-digit');
        var $prev_otp_digit = $otp_digit.prev();
        var $next_otp_digit = $otp_digit.next();

        // Backspace or left arrow key
        if (e.keyCode === 8 || e.keyCode === 37) {
            if ($prev_otp_digit.length && $prev_otp_digit.hasClass('otp-digit')) {
                $prev_otp_digit.find('input').select().focus();
            }
        }
        // Right arrow and numbers
        else if (e.keyCode === 39 || (e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
            if ($next_otp_digit.length && $next_otp_digit.hasClass('otp-digit')) {
                $next_otp_digit.find('input').select().focus();
            }
        }

        // Backspace
        if (e.keyCode === 8) {
            $input.val('');
        }
        else if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode >= 96 && e.keyCode <= 105)) {
            var characterCode = ((96 <= e.keyCode && e.keyCode <= 105)? e.keyCode - 48 : e.keyCode);
            $input.val(String.fromCharCode(characterCode));
        } else {
            return false;
        }
    });

    $input.on('keypress', function(e) {
        var keyCode = e.keyCode || e.which;

        // Allow only typing numbers
        if (keyCode >= 48 && keyCode <= 57) {
            return true;
        }

        return false;
    });

    $input.on("change",function(e) {
        var $this = $(this);
        var input_value = $this.val().replace(/[^0-9]/gi, '');

        $this.val(input_value);
        
        var $form = $this.closest('.gigya-otp-login-form');
        var $inputs = $form.find('.otp-digit input');
        var otp_code = '';

        $inputs.each(function() {
            otp_code += $(this).val();
        });
    });

    $input.on("cut copy paste",function(e) {
        e.preventDefault();
    });
});

If the jQuery .select() or segmenting it into multiple input isn't possible, maybe someone can give me an alternate solution.

I forked your code here and made some changes. Here are those changes:

  1. Changed input type text to number :
  2. Added CSS to hide the arrow keys of number inputs.

There are no changes in JS.

HTML

<div class="otp-group">
    <div class="otp-digit otp-digit-1">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-2">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-3">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-4">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-5">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>

    <div class="otp-digit otp-digit-6">
        <input type="number" class="input-text" autocomplete="on" aria-label="-" placeholder="-" aria-required="false" maxlength="1" pattern="\d*">
    </div>
</div>

Extra CSS

/* Add CSS to hide number arrows */
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Firefox */
input[type=number] {
  -moz-appearance: textfield;
}

By making these changes my mobile device started showing the number pad instead of a full keyboard which is a better UX for OTP inputs and your code already takes care to allow only numeric inputs so it's a win-win.

I didn't like the arrow keys so I kept them hidden using CSS. I'm not sure if that CSS works on iOS devices cause you might know safari doesn't support a lot of things in CSS.

And Voila. it started working on my phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM