zoukankan      html  css  js  c++  java
  • 只能输入数字0-9 正则表达式,兼容Google Firefox IE浏览器

    在Web页面输入框,只能输入数字,这个可以使用JS完成,但使用正则表达式更方便,查找了下有用onkeypress()和oninput()这两个事件来处理,我都尝试了下,结果如下:

    onkeypress="return (/[d$]/.test(String.fromCharCode(event.which||event.KeyCode)))" 
    
    oninput='this.value = this.value.replace(/D+/g, "")' 
    
    //或者
    <input oninput="CheckVal(this);"  type= "text" />
    function CheckVal(obj)
    {
       obj.value = obj.value.replace(/D+/g, "");
    }
    
    

    上面是我用了Google Firefox IE 3个浏览器测试的结果,onkeypress()事件对于3个浏览器都没有作用,而oninput()事件对3个浏览器都可以兼容实现。测试的比较急,有兴趣的小伙伴可以在测试下,不对的地方请指出。

  • 相关阅读:
    R的农场 chebnear
    math
    求平面内最近点对
    字符加密 cipher
    CF448C Painting Fence
    CF264B Good Sequences
    洛谷3166 数三角形
    [NOIP2013] 华容道
    [NOIP2013] 货车运输
    [NOIP2013] 积木大赛
  • 原文地址:https://www.cnblogs.com/wangqilong/p/12540404.html
Copyright © 2011-2022 走看看