当前位置: 代码迷 >> JavaScript >> 用js进展正负小数及范围判断
  详细解决方案

用js进展正负小数及范围判断

热度:181   发布时间:2012-10-26 10:30:59.0
用js进行正负小数及范围判断

最近项目中遇到一个js判断:允许输入正负数、小数,并且输入值有范围限制,而且相互之间有大小比较。

?

function checkNumber(id){
  var eValue = document.getElementById(id).value;
  //alert(value);
					
  if (/^(\+|-)?\d+($|\.\d+$)/.test(eValue)){//数字判断
    if(id == 'West' || id == 'East'){
      //alert(value);
      if((-180 <= eValue) && (eValue <= 180)){//范围判断
        return true;
      }else{
        alert("The west/east coordinate should be in -180 to 180");
        document.getElementById(id).value="";
        document.getElementById(id).focus();
        return false;
      }
    }else if(id == 'North' || id == 'South'){
        //alert(value);					 
        if((-90 <= eValue) && (eValue <= 90)){
          return true;
        }else{
          alert("The north/south coordinate should be in -90 to 90");
          document.getElementById(id).value="";
          document.getElementById(id).focus();
          return false;
        }
      }
    }else {
      if(document.getElementById(id).value != ""){
        alert(id +" Numbers only!");
        document.getElementById(id).value="";
        return false;
    }
  }
}


<tr>
  <td class="TDstyle01" align="center">North-South:
  <input type="text" id="North-South" name="NorthSouth" style='width:80px' onBlur="checkNumber('North-South')" >
  </td>
</tr>

<tr>
<td class="TDstyle01" align="left">East-West:&nbsp;&nbsp
<input type="text" id="East-West" name="EastWest" style='width:80px' onBlur="checkNumber('East-West')">
</td>
</tr>

?

  相关解决方案