限制为整数
<html> <head> <script language="javascript" type="text/javascript"> var G=document.getElementById; function checkNumber(){ var reg = /^(?:0|[1-9][0-9]?|100)$/; var strNumber = G("txtNumber").value; alert(reg.test(strNumber)) } </script> </head> <body > <input type="text" value="" id="txtNumber" title="Please enter a number witch between 0 and 100 .."> <button>CheckNumber</button> </body> </html>
限制为货币
<script language="JavaScript" type="text/javascript"> <!-- function checkMoney(obj){ var tempValue=obj.value.replace(/(^s+)|(s+$)/g,''); if(!tempValue){return;} if(/^-?d+(.d+)?$/.test(tempValue)){ obj.value=parseFloat(tempValue).toFixed(2); }else{ alert('请输入合法的货币值!'); obj.select(); return; } } //--> </script> onblur="checkMoney(this)"