下面我使用了我的代码,我想只允许单个数字或两个小数点,如下所示。
有效号码。
1
2.34
9.78
号码无效。
10
1.234
99.1234
用户无法插入无效号码。
我使用下面的代码。
<script src="mos-css/jquery-latest.js"></script>
<script>
document.write('<input id="inputField" onkeyup="run(this)" />');
function run(field) {
setTimeout(function() {
// var regex = /^\d{1}\.\d{2}$/;
// var regex = /^[0-9]+(\.[0-9]{5})?$/;
var regex =/^[0-9](\.\d{2})?$/;
///^\d+(\.\d{5})?$/;
field.value = regex.exec(field.value);
// field.value = regex.exec(field.value);
}, 5);
}
</script>
全部谢谢。
答案 0 :(得分:2)
<html>
<head>
<html>
<head>
<script>
// Retrieve last key pressed. Works in IE and Netscape.
// Returns the numeric key code for the key pressed.
function getKey(e)
{
if (window.event)
return window.event.keyCode;
else if (e)
return e.which;
else
return null;
}
function restrictChars(e, obj)
{
var CHAR_AFTER_DP = 2; // number of decimal places
var validList = "0123456789."; // allowed characters in field
var key, keyChar;
var char=0;
key = getKey(e);
if (key == null) return true;
// control keys
// null, backspace, tab, carriage return, escape
if ( key==0 || key==8 || key==9 || key==13 || key==27 )
return true;
// get character
keyChar = String.fromCharCode(key);
// check valid characters
if (validList.indexOf(keyChar) != -1)
{
// check for existing decimal point
var dp = 0;
if(obj.value.indexOf( ".")==-1){
// room for more after decimal point?
if( obj.value.length - dp <= char) {
return true; }
else{
if(keyChar == ".")
return true;
else
return false;
}
}
if((dp = obj.value.indexOf( ".")) > -1)
{
if(keyChar == ".")
return false; // only one allowed
else
{
// room for more after decimal point?
if( obj.value.length - dp <= CHAR_AFTER_DP)
return true;
}
}
else {return true};
}
// not a valid character
return false;
}
</script>
</head>
<body>
<form name="test">
Input Money Amount $<input type="text" name="money" onKeyPress="return restrictChars(event, this)">
</form>
</body>
</html>
试试代码
答案 1 :(得分:1)
为什么不接受所有输入,然后执行:
field.value.toFixed(2);
答案 2 :(得分:1)
您可以使用:
/^\d(?:\.\d{2})?$/
示例:
if (/^\d(?:\.\d{2})?$/.test(myString)) alert('true');
else alert('false');
答案 3 :(得分:1)
我尝试了ankit的代码并发现它无法正常工作,因此在下面包含了一个固定版本:
function getKey(e)
{
if (window.event)
return window.event.keyCode;
else if (e)
return e.which;
else
return null;
}
function restrictChars(e, obj)
{
var CHAR_AFTER_DP = 2; // number of decimal places
var validList = "0123456789."; // allowed characters in field
var key, keyChar;
key = getKey(e);
if (key == null) return true;
// control keys
// null, backspace, tab, carriage return, escape
if ( key==0 || key==8 || key==9 || key==13 || key==27 )
return true;
// get character
keyChar = String.fromCharCode(key);
// check valid characters
if (validList.indexOf(keyChar) != -1)
{
// check for existing decimal point
var dp = 0;
if(obj.value.indexOf( ".")==-1){
return true;
}
if((dp = obj.value.indexOf( ".")) > -1)
{
if(keyChar == ".")
return false; // only one allowed
else
{
// room for more after decimal point?
if( obj.value.length - dp <= CHAR_AFTER_DP)
return true;
}
}
else {return true};
}
// not a valid character
return false;
}