胡开明 用正则表达式控制价格输入
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>用正则表达式控制价格输入</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <script type="text/javascript"> var record={ num:"" } var checkDecimal=function(n){ var decimalReg=/^\d{0,8}\.{0,1}(\d{1,2})?$/;//var decimalReg=/^[-\+]?\d{0,8}\.{0,1}(\d{1,2})?$/; if(n.value!=""&&decimalReg.test(n.value)){ record.num=n.value; }else{ if(n.value!=""){ n.value=record.num; } } } </script>
- </head>
- <body>
- <h3>实用技巧:用正则表达式控制价格输入</h3>
- 销售价<input maxlength="10" size="18" type="text" name="price" value="" onkeyup='checkDecimal(this)'/>
- </body>
- </html>