能否给我一个数字验证的代码
只能输入数字,数字格式可以为整数,或者小数,小数位数最多为2位,谢谢!

解决方案 »

  1.   

    http://yulipeng.anyp.cn/040504085053515.aspx
    去这个网址看看~~
      

  2.   

    function isEmpty(str){
      if((str==null)||(str.length==0)) return true;
      else return false;
    }function isInt(theStr){
      var flag=true;
      if(isEmpty(theStr)){
        flag=false;
      }
      else{
        for(var i=0;i<theStr.length;i++){
          if(isDigit(theStr.substring(i,i+1))==false){
            flag=false;
            break;
          }
        }
      }
      return(flag);
    }function isBetween(val,lo,hi){
      if((val<lo)||(val>hi)){
        return false;
      }else{return true;}
    }function isDigit(theNum){
      var theMask='0123456789';
      if(isEmpty(theNum)) return false;
      else if(theMask.indexOf(theNum)==-1) return false;
      return true;
    }function isDate(theStr){
      var the1st=theStr.indexOf('-');
      var the2nd=theStr.lastIndexOf('-');
      if(the1st==the2nd){return(false);}
      else{
        var m=theStr.substring(the1st+1,the2nd);
        var d=theStr.substring(the2nd+1,theStr.length);
        var y=theStr.substring(0,the1st);
        var maxDays=31;
        if(isInt(m)==false||isInt(d)==false||isInt(y)==false){
          return false;
        }else if(y.length<4){
          return false;
        }else if(!isBetween(m,1,12)){
          return false;
        }else if(m==4||m==6||m==9||m==11) maxDays=30;
        else if(m==2){
          if(y%4>0) maxDays=28;
          else if(y%100==0&&y%400>0) maxDays=28;
          else maxDays=29;
        }
        if(isBetween(d,1,maxDays)==false){
          return false;
        }else{return(true);}
      }
     }
      

  3.   

    http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5