javascript Javascript禁用更改密码文本框的空格键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18780304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:12:59  来源:igfitidea点击:

Javascript disable space key for change password textboxes

javascriptkeyboardpasswords

提问by user2706372

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

用户不得在 2 个文本框内的键盘中使用空格。

1.textbox YeniSifreTextBox_I

1.文本框YeniSifreTextBox_I

2.textbox YeniSifreTekrarTextBox_I

2.文本框YeniSifreTekrarTextBox_I

回答by Ashis Kumar

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

您可以使用以下 javaScript 代码来阻止空格键,

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

HTML

HTML

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

Hope this helps you.

希望这对你有帮助。

回答by Abhishek Gautam

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

您可以实现相同的功能,而无需创建自定义函数。