用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>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值