87,901
社区成员
发帖
与我相关
我的任务
分享
<style type="text/css">
*{ margin: 0; padding: 0; } </style>
<!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>
<title></title>
<script src="../script/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.fn.extend({
decimal: function(len, x) {
var str;
if (x == 0) {
str = "^(?!0{2,})(?!0\\d+)\\d{0," + len + "}$"; //小数位0
} else if (x == 1) {//小数位1
str = "^(?!0{2,})(?!0\\d+)(?:\\d{1," + (len - x) + "}\\.?[\\d+]{0," + x + "})(?!\\.)$";
} else {//decimal(12,2) 有效长度12,小数位2
str = "^(?!0{2,})(?!0\\d+)(?:\\d{1," + (len - x) + "}\\.?[\\d+]{0," + x + "}|\\d{1," + ((len - x) + 1) + "}\\.?[\\d+]{0," + (x - 1) + "})(?!\\.)$";
}
var reg = new RegExp(str);
this.bind("propertychange", function() {
if (this.value.length > 0) {
if (!reg.test(this.value)) {
this.value = this.value.substring(0, this.value.length - 1);
}
}
return true;
});
return this;
}
});
window.onload = function() {
$("input[type='text']").decimal(3, 0);
};
</script>
</head>
<body>
<input type="text" />
</body>
</html>