<s:textfield name="num" οnkeyup="value=value.replace(//b0+(?=[1-9]/d*|(?!/b))/g,'')" />
只能输入正整数。
不正确数值:001,
0.0,
0 0等非法字符
输入的数字,自动添加逗号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<form>
<input type="text" name="num" οnkeyup="value=value.replace(//b0+(?=[1-9]/d*|(?!/b))/g,'')" />
</form>
</body>
</html>
<script>
$("input").keyup(function(){
var v, j, sj, rv = "";
v = $(this).val().replace(/,/g,"").split(".");
j = v[0].length % 3;
sj = v[0].substr(j).toString();
for (var i = 0; i < sj.length; i++)
{
rv = (i % 3 == 0) ? rv + "," + sj.substr(i, 1): rv + sj.substr(i, 1);
}
var rvalue = (v[1] == undefined) ? v[0].substr(0, j) + rv: v[0].substr(0, j) + rv + "." + v[1];
if (rvalue.charCodeAt(0) == 44)
{
rvalue = rvalue.substr(1);
}
$(this).val(rvalue);
});
</script>