简体   繁体   English

替换文本框中数字以外的字符

[英]Replacing Characters other than numbers in textbox

I want to allow only numbers(1 to 5), a $(dollar) .(decimal) and '(singlequote) in a textbox. 我只允许在文本框中输入数字(1到5),$(美元),(十进制)和'(单引号)。 I want to do this on keyup event. 我想在keyup事件上执行此操作。 If user types anything other than these character, it should be replaced with a *. 如果用户键入除这些字符以外的任何其他字符,则应将其替换为*。 Please tell how can I do it? 请告诉我该怎么做?

You can strip the string on key-up like this: 您可以像这样在键入时剥离字符串:

$('input[type=text]').keyup(function(){
  $(this).val($(this).val().replace(/[^1-5$.']/g, '*'));
});

Keep in mind that you should verify this on the server side (there are a plethora of ways around it on the client side). 请记住,您应该在服务器端对此进行验证(客户端有很多解决方法)。

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

相关问题 Javascript正则表达式,替换除数字以外的所有字符 - Javascript regex, replace all characters other than numbers javascript正则表达式以检查字符和数字以外的其他字符 - javascript regular expression to check other than characters and numbers 具有符号,字符和数字限制的文本框 - TEXTBOX with Restriction in Symbols,Characters and Numbers RegEx在Javascript中替换大于变量的数字 - RegEx replacing numbers greater than variable in Javascript 如何仅在 vuejs 中使用带有字符和数字的文本框? - How to use textbox with characters and numbers only in vuejs? 在一个正则表达式中为数字添加逗号,替换字符并添加空格 - Adding commas to numbers, replacing characters and adding a space all in one regex 仅在文本框中允许中文字符和英文数字-Javascript - Allow chinese characters and English numbers only in textbox - Javascript 要检查的正则表达式-不超过2个连续数字或字符,并且不超过1个相同数字或字符 - Regular Expression to check- No more than 2 sequential numbers or characters and No more than 1 same numbers or characters-Javascript 用js中数组中的其他字符替换特殊字符 - replacing special characters with other character from array in js 正则表达式如何在不替换任何其他数字的情况下替换 $value - Regex How to replace a $value without replacing any other numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM