zoukankan      html  css  js  c++  java
  • JS判断是否为数字类型


     

    1.isNaN是用来判断是否为数字,比如1,1.05之类的。   

           function IsNumber(obj)
            {
               if(isNaN(obj.value))
                {
                   alert('
    必须为金钱类型!');
                }
            }

    2.用正则表达式判断是否为整数
            function BASEisNotInt(obj){
            //
    判断是否为整数
                var re =/^[1-9]+[0-9]*]*$/;              //判断字符串是否为数字 ^[0-9]+.?[0-9]*$/
           if (!re.test(obj.value))
                {
                   alert("
    请输入数字(例:1)!");
                 }
            }

    下面为普通函数写法

    function BASEisNotNum(theNum)
    {
    //
    判断是否为数字
    if(BASEtrim(theNum)=="")
    return true;
    for(var i=0;i<theNum.length;i++){
    oneNum=theNum.substring(i,i+1);
    if (oneNum<"0" || oneNum>"9")
    return true;
    }
    return false;
    }

    function BASEisNotInt(theInt)
    {
    //
    判断是否为整数
    theInt=BASEtrim(theInt);
    if ((theInt.length>1 && theInt.substring(0,1)=="0") ||BASEisNotNum(theInt)){
    return true;
    }
    return false;
    }

    function BASEisNotFloat(theFloat)
    {
    //
    判断是否为浮点数
    len=theFloat.length;
    dotNum=0;
    if (len==0)
    return true;
    for(var i=0;i<len;i++){
    oneNum=theFloat.substring(i,i+1);
    if (oneNum==".")
    dotNum++;
    if ( ((oneNum<"0" || oneNum>"9") && oneNum!=".")|| dotNum>1)
    return true;
    }
    if (len>1 && theFloat.substring(0,1)=="0"){
    if (theFloat.substring(1,2)!=".")
    return true;
    }
    return false;
    }

  • 相关阅读:
    虚拟机镜像下载
    The method getTextContent() is undefined for the type Node
    sun.misc.BASE64Encoder找不到jar包的解决方法
    eclipce项目中的js报错解决
    eclipce导入项目导入不进去
    myeclipse新安装好后需要的设置
    mysql软件下载
    Git Bash 命令行方式 提交源码到GitHub
    使用plsql developer 创建用户
    鼠标滑过TAB选项卡切换demo 可拓展
  • 原文地址:https://www.cnblogs.com/liaoshiyong/p/3150890.html
Copyright © 2011-2022 走看看