<input type="textbox" id="test" onkeypress="" />求onkeypress的一正则表达式,
要求只能输入数字、小数点、逗号、加减号。加减号输入位置只能在数字的最前方或者最后方。
谢谢。

解决方案 »

  1.   

    onkeypress 里 只能 用 keyCode 来判断范围
    <input type="textbox" id="test" onkeyup="return fn2(event,this)" onkeydown="this.bak=this.value" onkeypress="return fn(event,this)" />
    </body>
    <script type="text/javascript">
     function fn(evt,el){
       var c=evt.keyCode;
       console.info(c)
       var b=(c>47 && c<58)||c==43 ||c==45
     
       if(!b) return b;
     }
     function fn2(evt,el){
       if(!/^[\+\-]?\d+[\+\-]?$/.test(el.value)) el.value=el.bak||'';
      return false;
     }</script>
      

  2.   

    注CSDN论坛微博送可用分CSDN论坛微博主要发布程序员职场经验,生活指南,IT八卦等内容,关注就
      

  3.   

    <input type="text" onkeyup="this.value=this.value.replace(/[^\d\.,\+-]/g,'')" onblur="if(!/^[\+-]?[\d\.,]+[\+-]?$/g.test(this.value))this.value=''" />