日期:2014-05-16 浏览次数:20386 次
最近项目中遇到一个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:   <input type="text" id="East-West" name="EastWest" style='width:80px' onBlur="checkNumber('East-West')"> </td> </tr>
?