简体   繁体   English

如何将文本输入仅限制为数字,但允许使用10键输入数字?

[英]How do I restrict a text input to numbers only but allow numbers from a 10 key?

I am confused why I am not able to use the 10 key to input numbers. 我很困惑为什么我不能使用10键来输入数字。

The following example auto fills the "/". 以下示例自动填充“ /”。 I tried a few different ways to adjust the regex on it, but when I do, I can't backspace or delete anything past the last "/". 我尝试了几种不同的方法来调整正则表达式,但是当我这样做时,不能退格或删除最后一个“ /”之后的内容。 (01/01/2000 ... 01/01/ <- won't let me delete past this). (2000年1月1日... 01/01 //-不允许我删除过去的内容)。

       $('#date').keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            $date = $(this);

            if (key !== 8) {
                var regex = new RegExp("^[0-9\t/]");
                var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
                if (!regex.test(key)) {
                   event.preventDefault();
                   return false;
                }

                if ($date.val().length === 0) {
                    $date.val($date.val() + '');
                }
                if ($date.val().length === 2) {
                    $date.val($date.val() + '/');
                }
                if ($date.val().length === 5) {
                    $date.val($date.val() + '/');
                }
            }
        }); 

Please help me find a way where it continues to auto-format the "/" as it does now but restricts to numbers only (except the required "/") and allows me to use the 10 key numbers. 请帮助我找到一种方法,该方法可以像现在一样继续自动格式化“ /”,但仅限于数字(必需的“ /”除外),并允许我使用10个键数字。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you in advance. 先感谢您。

if you want allow only number and ignore any key : use this code in JavaScript tag: 如果您只允许数字而忽略任何键:请在JavaScript标记中使用以下代码:

 $('*[data-validation="digit"]').keydown(function (e) {
                if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
                        (e.keyCode == 65 && e.ctrlKey === true) ||
                        (e.keyCode >= 35 && e.keyCode <= 40)) {
                    return;
                }
                if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                    e.preventDefault();
                }
            });

and any input that you want apply this role add this attribute : 以及您想要应用此角色的所有输入都添加以下属性:

data-validation="digit"

for example : 例如 :

<input data-validation="digit" /> 

I recommend you Use a Masked Input plugin eg: Masked Input plugin for jQuery 我建议您使用Masked Input插件,例如: jQuery的Masked Input插件

for example for date : 例如日期:

 $("#date").mask("99/99/9999",{placeholder:"mm/dd/yyyy"});

in this plugin you can combination of word and number or any characters 在此插件中,您可以将单词和数字或任何字符组合在一起

You're not checking for backspace properly. 您没有正确检查退格键。 Check your value of 'key'. 检查您的“密钥”值。

I think it should assigned as followed: 我认为应该分配如下:

var key = e.keyCode;

Also, why is half of it e and later on you have event ? 另外,为什么一半是e以及以后发生event

var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);

This might also be useful for JS reference: 这对于JS参考也可能有用:

https://css-tricks.com/snippets/javascript/javascript-keycodes/ https://css-tricks.com/snippets/javascript/javascript-keycodes/

You might also want to use jQuery's which . 您可能还想使用jQuery的which It should work across browsers. 它应该可以跨浏览器工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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