Search code examples
javascriptkeyboardpasswords

Javascript disable space key for change password textboxes


,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


Solution

  • 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.