简体   繁体   English

如何删除keyup上的某些字符

[英]How to remove certain characters on keyup

I'd like to dynamically replace all non-numeric input as the user types. 我想在用户输入时动态替换所有非数字输入。 I've got the input type set to number , but that still allows the user to type non-numbers, the browser just flags it. 我已将输入类型设置为number ,但仍然允许用户键入非数字,浏览器只标记它。

Instead of just flagging non-numbers (for exampl, commas, spaces, or dashes), I'd like to simply remove them as the user types. 我不想只标记非数字(例如,逗号,空格或短划线),而是在用户输入时简单地删除它们。 The code listed here, which is very straightforward, is completely deleting the string if a non-number is encountered, which is not what I want. 这里列出的代码非常简单,如果遇到非数字,则完全删除字符串,这不是我想要的。

 var cc = document.querySelector('input.credit-card-number'); cc.addEventListener('keyup',function(){ this.value = this.value.replace(/[^0-9]/g,''); }); 
 <input type="number" class="credit-card-number"> 

Edit 编辑

This is a Firefox-only problem. 这是仅限Firefox的问题。 Chrome seems to work as expected. Chrome似乎按预期工作。 I'd still appreciate a fix if anyone can come up with one. 如果有人能想出一个,我仍然会感激。

  1. Sticking with javascript only, a slight modification will make this work. 仅使用javascript ,稍作修改即可实现此功能。 Change the input type from number to text . 将输入类型从number更改为text

 var input = document.querySelector('input.credit-card-number'); input.addEventListener('keyup', function(e) { this.value = this.value.replace(/[^0-9]/, ''); }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <input type="text" class="form-control credit-card-number" placeholder="Ex: 0000 0000 0000 0000" /> 

  1. Don't look like you want jquery solution but if you want to use one you can go for input masking for which there is a library too and support various masking options like Credit Card , IP Address and Mobile Numbers you can see a demo for the credit card concerned section below I used firefox to create and test the demo. 看起来你不想要jquery解决方案,但如果你想使用它,你可以去输入屏蔽,也有图书馆,并支持各种屏蔽选项,如Credit CardIP AddressMobile Numbers你可以看到一个演示信用卡有关部分下面我使用firefox来创建和测试演示。

 //Credit Card $('.credit-card-number').inputmask('9999 9999 9999 9999', { placeholder: '____ ____ ____ ____' }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/jquery.inputmask.bundle.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.3.4/css/inputmask.css" rel="stylesheet" /> <input type="text" class="form-control credit-card-number" placeholder="Ex: 0000 0000 0000 0000" /> 

Update 更新

Firefox looks at this.value and says, "Ok, I'll just take the whole value of whatever is in the input and overwrite the whole value ." 火狐看着this.value ,说:“好吧,我就利用无论是在输入的整体价值 ,并覆盖整个价值 。” Chrome somehow intuitively interprets keydown the way you intended, but Firefox literally follows your code truthfully. Chrome以某种方式直观地按照您的意图解释了keydown,但Firefox真实地遵循您的代码。 Unfortunately, that's not what you want, so you'll need to take a different approach with Firefox. 不幸的是,这不是你想要的,所以你需要采用与Firefox不同的方法。

Instead of comparing strings, try comparing keystrokes a character at a time. 不要比较字符串,而是尝试一次比较一个字符的击键。 keyboardEvent.keycode for Chrome and keyboardEvent.which for Firefox. keyboardEvent.keycode Chrome和keyboardEvent.which为Firefox。 See Demo 2 . 演示2 BTW, keydown event is usually more reliable, if for some reason keyup is one off on results, keydown is probably better. BTW,keydown事件通常更可靠,如果由于某种原因keyup是一次性结果,keydown可能更好。 If keydown needs 2 keystrokes to work, then try listening on capture phase instead of the bubble phase by setting the third parameter to true . 如果keydown需要2次击键才能工作,那么通过将第三个参数设置为true来尝试监听捕获阶段而不是气泡阶段。

DOMObject.addEventListener('keydown', funk, true)

Change regex to: 将正则表达式更改为:

/\D/gi

\\D means anything that is not a digit and the i flag is for case insensitivity. \\D表示不是数字的任何内容,而i标志表示不区分大小写。

Demo 1 演示1

 var cc = document.querySelector('input.credit-card-number'); cc.addEventListener('keyup', function() { this.value = this.value.replace(/\\D/gi, ''); }); 
 <input type="number" class="credit-card-number"> 

Demo 2 演示2

charCode > 47 && charCode < 58 ......0 to 9
charCode > 95 && charCode < 105......0 to 9 scroll locked 10-key pad
charCode == 8........................← Backspace

 var cc = document.querySelector('input.credit-card-number'); cc.addEventListener('keydown', function(e) { var charCode = (e.which) ? e.which : e.keyCode; if ((charCode > 47 && charCode < 58) || (charCode > 95 && charCode < 105) || charCode == 8) { return true; } e.preventDefault(); return false; }, true); 
 <input type="number" class="credit-card-number"> 

Just change the input type from number to text. 只需将输入类型从数字更改为文本即可。

edit: At least in Mozilla Firefox it works. 编辑:至少在Mozilla Firefox中它可以工作。 See here 看这里

https://jsfiddle.net/h9ap2ag8/1/ https://jsfiddle.net/h9ap2ag8/1/

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input type="text" class="credit-card-number">
</body>
</html>

var cc = document.querySelector('input.credit-card-number');
cc.addEventListener('keyup',function(){
    this.value = this.value.replace(/[^0-9]/g,'');
});

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

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