[英]how to check that user can not input more than one decimal or dot in html web page
我有一个带有输入字段的网页,我希望用户可以在其中输入数字,但数字不能包含两位小数,例如10.0.0,意味着它只能输入。一次而不是我们如何解决此问题的两倍。
function run(element) {
var regex = /\d*\.?\d?/g;
element.value = regex.exec(element.value);
}
call this function on keypress
在
keypress
上调用此功能
You could handle the event of the keypress or keyup and check if the key pressed is a dot. 您可以处理按键或按键事件,并检查按键是否为点。 If it is entered the first time, you could assign a boolean centinel to true, and cancel the keypress event for future dots.
如果是第一次输入,则可以将布尔centinel设置为true,并取消将来的点的keypress事件。
Presuming input element el is already assigned to a variable v. 假定输入元素el已分配给变量v。
if (1 < v.match(/\./g).length) {
alert("Multidots found");
}
Try using 尝试使用
var str = '10.0.0';
str.indexOf('.');
which gives how many times its contain in a string. 它给出了它在一个字符串中包含多少次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.