zoukankan      html  css  js  c++  java
  • JS判断是否为数字,是否为整数,是否为浮点数[转]

    JS判断是否为数字,是否为整数,是否为浮点数

    正则表达式方法

    function checkRate(input)
    {
         var re = /^[0-9]+.?[0-9]*$/;   //判断字符串是否为数字     //判断正整数 /^[1-9]+[0-9]*]*$/  
         if (!re.test(input.rate.value))
        {
            alert("请输入数字(例:0.02)");
            input.rate.focus();
            return false;
         }
    }

    下面为普通函数写法

    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;
    }

  • 相关阅读:
    Centos 设置开机进图形界面/终端
    Ubuntu 开机慢(networking.service导致)
    Linux 内核下载地址
    C/C++中内存对齐
    编译器数据模型
    CPU中断
    sql语句
    mysql学习
    Active进阶
    SpringBoot整合ActiveMQ
  • 原文地址:https://www.cnblogs.com/Hdsome/p/1232819.html
Copyright © 2011-2022 走看看