zoukankan      html  css  js  c++  java
  • JS 数字校验

    //-----------------------------------------------函数(1):允许输入正数和负数的表达式-----------------------------------

    function (num){

       var reg = /^(-|+)?(d{1,8})?([.]d*)?$/;

       re.test(num)

    }

    //------------------------------------------------函数(2):允许输入正数和负数的表达式---------------------------------------------

    function (num){

      var reg = /^(-|+)?d{0,8}([.]d*)?$/;    re.test(num)

    }
                      这里之所以d{0,8}是因为(-|+)?可以允许有或者没有,当不输入的时候如果是d{1,8}则需要确保必须有一个数字,但此时是没有

    输入的

    //------------------------------------------------函数(3):数字类型------------------------------------------------------

    function NumberCheck(num) {
                  var re=/^d*.{0,1}d*$/;
                  return re.exec(num) != null;
                }
    function  function checkNum(obj){
                 if(!NumberCheck(obj.value)){
             alert("格式不对,请输入数字类型");
            }

    //--------------------------------------------------函数(4):数字类型------------------------------------

    function  function checkNum(obj){

      obj.value=obj.value.replace(/[^d.]/g,"")

    }

    //--------------------------------------------------函数(5):数字类型------------------------------------

    function  function checkNum(obj){
      this.value=this.value.replace(/[^0-9]D{1,10}([.]d{0,2})?$/,"")

    }

    //--------------------------------------------------函数(6):数字8位整数两位小数类型----------------------------------------------------

    function NumberCheck(num)  {
               var re=/^d{1,8}([.]d{0,2})?$/;
                   return re.exec(num) != null;
                  }
    function checkNum(obj){
        if(!NumberCheck(obj.value)){
             alert("格式不对,请输入数字8位整数两位小数类型");
            }

    //---------------------------------------------------函数(7):10以内的带小数的数字---------------------------------------------

    function NumberCheck(num)  {

      var re=/^([1-9]([.]d*)?||10)$/ig;

      return re.exec(num);

    }

    //-----------------------------------------------触发事件----------------------------------------------
    onkeyup="checkNum(this);"

           /i  不区分大小写 insensitive
                    /g 全局匹配 global
                    /m 多行模式 multi
                    /gi 和/ig  就是/i 和/g的组合

  • 相关阅读:
    我眼中的DevOps
    Jenkins常用插件介绍之权限控制插件Role-based Authorization Strategy
    sql查询一个班级中总共有多少人以及男女分别多少人
    win8 图片等路径
    WPF 设置TextBox为空时,背景为文字提示。
    WCF服务发布
    win8 摄像
    oracle 删除主键
    oracle 数据库连接
    oracle 创建用户表
  • 原文地址:https://www.cnblogs.com/fenger-VIP/p/14777095.html
Copyright © 2011-2022 走看看