Javascript disable space key for change password textboxes

13,417

Solution 1

You can use the below javaScript code to block the space key,

function RestrictSpace() {
    if (event.keyCode == 32) {
        return false;
    }
}

HTML

<textarea onkeypress="return RestrictSpace()"></textarea>

Hope this helps you.

Solution 2

                        <input type="number" 
                           id="cardNumber"
                           name="cardNumber"
                           required
                           onKeyDown="if(event.keyCode === 32)
                           return false;">

You can implement the same functionality, without creating a custom function.

Share:
13,417

Related videos on Youtube

user2706372
Author by

user2706372

Updated on May 31, 2022

Comments

  • user2706372
    user2706372 almost 2 years

    ,Hi all,

    var veri = {
    YeniSifreTextBox: $('#YeniSifreTextBox_I').val(),
    
    YeniSifreTekrarTextBox: $('#YeniSifreTekrarTextBox_I').val(),
    };
    
    if (veri.YeniSifreTextBox == '' || veri.YeniSifreTekrarTextBox == '') {
    alert("Password Can not be empty!");
    }
    else if (veri.YeniSifreTextBox != veri.YeniSifreTekrarTextBox) {
    alert("Passwords dont not match !");
    }
    

    I can control password can not be empty and passwords dont not match with above codes.

    I want to disable to enter space key while user write password.

    User must never use space in keyboard inside of 2 textboxes.

    1.textbox YeniSifreTextBox_I

    2.textbox YeniSifreTekrarTextBox_I

  • chetan
    chetan almost 8 years
    when i press space it won't allow. But this code is not working when i copy and paste spaces.