用js如何验证输入的数不能多于2位小数,多了就弹出提示框?

解决方案 »

  1.   


    function checknum(strValue,strlen) 

    var strValue; if(isNaN(strValue) == true){ return false; }else{ strValue = strValue.split("."); 
    if ((strValue[0].split(" ")[0]=="")||(strValue[0]=="")) 
    { return false; }else{ 
    if ((strValue[0].substr(0,1) =="-")||(strValue[0].substr(0,1) =="+")) 
    { return false; 
    }else{ 
    if (strValue[1]=="") 
    { return false; 
    }else{ 
    if (strValue[0].substr(0,1) == "0" && strValue[0].length > 1) 
    { return false; 

    else 

    if (strValue[1].length > strlen) 

    return false; 

    else 

    return true; 







    function check() 

    if (checknum(document.form1.t1.value,2) == false) 

    alert("不满足条件"); 

    else 

    alert("满足条件"); 


    </SCRIPT简单写的没测试
      

  2.   

    var indexD = txt.indexOf('.');
    var befStr = txt.substring(0 ,indexD-1);
    var aftStr = txt.substring(indexD +1,txt.length);
    if(aftStr.length>2 && indexD > 0)
    {
       alert("小数位最多两位!");
           return false;
    }
    return true;
      

  3.   

    var reg=/\.\d{0,2}$/
    if(reg.exec(value))
    return true;
    else return false;
      

  4.   

    var 数组名=NUM.split('.');
    if(数组名.lenght>1)
    {
     if(数组名[1].lenght>2)
     {
     alert('大于两位小数');
     }
    }
      

  5.   

    测试通过
    <script>
    function b()
    {
    var num=25.6987+'';
    var aa=num.split('.');
    if(aa.length>1)
    {
    if(aa[1].length>2)
    {
    alert(aa[1]);
    }
    }
    else
    {
    alert(aa[0]);
    }
    }
    </script>
    <body onload='b()'></body>