<!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>
</head>
<body>
<input type="text" id="test">
</body>
</html>
<script type="text/javascript">
window.onload = function(){
document.getElementById('test').onkeyup = function(){
if(this.value.length==1){
this.value=this.value.replace(/[^1-9]/g,'');
}else{
this.value=this.value.replace(/\D/g,'');
}
if(this.value.length>3){
this.value = this.value.substr(0,3);//限制最大数不能超过3位数
}
if(this.value.length ==1){
if(this.value==0){
this.value ='';//限制第一位数不能为0
}
}
}
document.getElementById('test').onafterpaste = function(){
if(this.value.length==1){
this.value=this.value.replace(/[^1-9]/g,'');
}else{
this.value=this.value.replace(/\D/g,'');
}
}
}
</script>