Limited to integers
<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 onclick="checkNumber();">CheckNumber</button> </body> </html>
Limited to currency
<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)"