Currently I am using following jQuery code to filter only digits:
目前我使用以下jQuery代码只过滤数字:
$('#input_field').keyup(function(e) {
if (/\D/g.test(this.value)) {
this.value = this.value.replace(/\D/g, '');
}
});
But I want to get floating point numbers(upto to 2 decimal places) like this:
但是我想要得到浮点数(上升到小数点后两位)
10.2
1.23
1000.10
5 个解决方案
#1
8
Try this regex:
试试这个正则表达式:
/^\d+(\.\d{0,2})?$/
Your JS:
JS:
$('#input_field').keyup(function(e) {
var regex = /^\d+(\.\d{0,2})?$/g;
if (!regex.test(this.value)) {
this.value = '';
}
});
#2
3
try
试一试
toFixed(2)
eg:
例如:
var number = 2.234239;
var numberfixed=number.toFixed(2);
#3
2
I think you have to use typing interval, because keyup is to quick and the regex don't approve something like this 0.
我认为您必须使用类型间隔,因为keyup是快速的,而regex不批准像这样的0。
var typingTimer;
var doneTypingInterval = 1000;
$('.myInputField').keyup(function(){
clearTimeout(typingTimer);
if ($('.myInputField').val) {
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
});
function doneTyping () {
var vale = $('.myInputField').val();
var regexTest = /^\d+(?:\.\d\d?)?$/;
var ok = regexTest.test(vale);
if(!ok){
$('.myInputField').val('');
}
}
http://jsfiddle.net/jWbsE/
#4
1
You need to change the regular expression used to test the values against.
您需要更改用于测试值的正则表达式。
/^\D+(\.\D\D?)?$/
This will allow numbers with no decimal point, or with a decimal point and one or two digits after.
这将允许不带十进制点的数字,或者带十进制点的数字和后面的一两个数字。
#5
0
var dot = fVal.split(".");
var len0 = 0;
var len1 = 0;
if (dot.length == 2) {
len0 = dot[0].length;
len1 = dot[1].length;
} else if (dot.length > 2)
len1 = 3;
else
len1 = 0;
var fValFlt = parseFloat(fVal);
var fValN = isNaN(fVal);
if ((len1 > 2) || fValN == true || fValFlt < 0) {
//failure arguments
} else {
//success arguments
}
In above code fVal is the field value for which you might be checking for.
在上述代码中,fVal是您可能要检查的字段值。
#1
8
Try this regex:
试试这个正则表达式:
/^\d+(\.\d{0,2})?$/
Your JS:
JS:
$('#input_field').keyup(function(e) {
var regex = /^\d+(\.\d{0,2})?$/g;
if (!regex.test(this.value)) {
this.value = '';
}
});
#2
3
try
试一试
toFixed(2)
eg:
例如:
var number = 2.234239;
var numberfixed=number.toFixed(2);
#3
2
I think you have to use typing interval, because keyup is to quick and the regex don't approve something like this 0.
我认为您必须使用类型间隔,因为keyup是快速的,而regex不批准像这样的0。
var typingTimer;
var doneTypingInterval = 1000;
$('.myInputField').keyup(function(){
clearTimeout(typingTimer);
if ($('.myInputField').val) {
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
});
function doneTyping () {
var vale = $('.myInputField').val();
var regexTest = /^\d+(?:\.\d\d?)?$/;
var ok = regexTest.test(vale);
if(!ok){
$('.myInputField').val('');
}
}
http://jsfiddle.net/jWbsE/
#4
1
You need to change the regular expression used to test the values against.
您需要更改用于测试值的正则表达式。
/^\D+(\.\D\D?)?$/
This will allow numbers with no decimal point, or with a decimal point and one or two digits after.
这将允许不带十进制点的数字,或者带十进制点的数字和后面的一两个数字。
#5
0
var dot = fVal.split(".");
var len0 = 0;
var len1 = 0;
if (dot.length == 2) {
len0 = dot[0].length;
len1 = dot[1].length;
} else if (dot.length > 2)
len1 = 3;
else
len1 = 0;
var fValFlt = parseFloat(fVal);
var fValN = isNaN(fVal);
if ((len1 > 2) || fValN == true || fValFlt < 0) {
//failure arguments
} else {
//success arguments
}
In above code fVal is the field value for which you might be checking for.
在上述代码中,fVal是您可能要检查的字段值。