[英]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.