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的组合

原文地址:https://www.cnblogs.com/kongxc/p/6474505.html