简体   繁体   English

输入数字掩码-防止减号

[英]input number mask - prevent minus sign

Trying to build an own input number mask for number with space as separator and no decimals. 尝试为数字创建自己的输入数字掩码,并使用空格作为分隔符,并且不使用小数。 Both positive and negative numbers allowed. 正数和负数均允许。 Solution below so far. 到目前为止,下面的解决方案。 One issue, how to not allow only a minus sign (input field should then be empty or 0) and trailing minus signs? 一个问题,如何不只允许减号(输入字段应为空或为0)和尾随的减号? Any other suggestions how to improve code? 还有其他建议如何改善代码?

 function thousandany(x) { return x .replace(/(?!-)\\D+/g, '') //ta bort alla utom nummer .toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g, " "); } $(document).ready(function() { $("input").keyup(function() { $(this).val(thousandany($(this).val())); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input style="text-align:right"> 

I really do not know much about regex, but your UX does not seem very user-friendly and confortable. 我对regex确实了解不多,但是您的UX似乎不太用户友好和舒适。 Why not run further validation on the data to see if it works fine? 为什么不对数据进行进一步的验证以查看其是否工作正常? You are forcing the user to type only on the right side. 您强迫用户仅在右侧键入。 If you want to change the text you have inputted, and it has 20 figures, you would have to remove all of them just to change the first one. 如果要更改输入的文本,并且该文本有20个数字,则只需删除所有数字即可更改第一个数字。

I would recommend, as an alternative approach, as I said, to run validation to the code after the user has finished typing the data (so it does not become annoying), or for example when it focuses out of the input. 正如我所说的那样,我建议作为一种替代方法, 在用户完成数据键入 (这样就不会变得令人讨厌)之后,或者例如在数据集中在输入之外时对代码运行验证。 The user is not idiot, if you tell him once that he has to enter, say, positive numbers, say, an integer, he will change the input accordingly. 用户不是白痴,如果您一次告诉他必须输入正数(例如整数),那么他将相应地更改输入。 To be constantly tricking his or her input can actually become annoying, especially if you cannot change the text you have already written. 要不断地欺骗他或她的输入实际上会变得很烦人,尤其是如果您无法更改已经编写的文本时。

You can check whether it is a valid number, whether it is integer, whether it is positive, etc. It makes much more sense for me than to be forcing the input in such a way, and it also makes the code much more complicated – when my opinion is that it is unnecessary. 您可以检查它是否为有效数字,是否为整数,是否为正数,等等。对于我来说,这比强制以这种方式强制输入更为有意义,并且这也使代码更加复杂-当我认为这是不必要的。

Also do not forget that there are other aspects such as scientific notation (which you might want to consider) which use also letters and would require a much more complex regex if you wanted to implement them at all. 同样不要忘记,还有其他方面,例如科学计数法(您可能想考虑)也使用字母,并且如果要实现它们,则需要更复杂的正则表达式。

Of course I know this does not directly answer your question, but it might make you re-think if the effort you are making is completely necessary or you can follow a more efficient approach. 当然,我知道这并不能直接回答您的问题,但是它可能使您重​​新考虑是否完全需要付出的努力,或者可以采用更有效的方法。

An example of how you could do it after the user focuses out or clicks a specific button, would be: 用户聚焦或单击特定按钮后如何执行此操作的示例如下:

$(document).ready(startCheck)

function startCheck() {

    $("#myInputField").focusout(checkData)
    %("#myButton").click(checkData)
}

function checkData() {
    var data = $("#myInputField").val();
    if $.isNumeric(data) {
        // do something
    } else {
        alert("Please input a number")
}

Of course you can change it on your convenience, to do a more user-friendly way of notifying that input is incorrect, and to adjust whether you want only integers to pass, or positive values to pass, etc. 当然,您可以方便地更改它,以一种更加用户友好的方式来通知输入错误,并调整是否只希望传递整数,还是只传递正值,等等。

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

相关问题 使用replace()减号和点清除输入类型编号字段 - Minus sign and dot clear input type number field with replace() HTML5输入类型编号不能阻止加号,减号 - Html5 input type number not prevent plus, minus symbols 如何用减号反转数字 - How to reverse number with minus sign 正则表达式使用 AngularJS 删除数字类型输入的加号和减号 - Regex to remove plus and minus sign of a number-type input using AngularJS 在jQuery中使用数字格式删除减号 - Minus sign removed using number formatting with jQuery 如何在JS中修复数字的减号 - How to fix minus sign of a number in JS 数字的掩码输入 - 百分比 - Mask input for number - percent Angular 7 指令在 Android 电话上用于特殊字符点和减号的输入类型“数字”的 keydown 事件不会阻止 - Angular 7 directive with keydown event of input type “number” not prevent on Android phone for special character dot and minus 允许在 integer 或特定长度的浮点数的实时输入验证期间在数字的开头键入减号,最多两位小数 - Allow typing minus sign at the start of a number during live input validation of integer or float of specific length with up to two decimal digits 文本输入:将输入限制为数字 (0-9) 和减号 (-)。 没有按预期工作 - Text Input: Limit input to numbers (0-9) and the minus sign (-). Not working as expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM