验证input输入值,并给出提示信息

 

<input type="text" name="requestedCorrectionHours" id="correctionInput" value="${wbViewBean.requestedCorrectionHours}" 
     οnblur="leaveToHandler('correctionInput');">
  
var ERROR_MESSAGE = ({
 LIMIT_NUMBER: '<uw:write textKey="label.text.validate.input.limitnumber"/>'
})

 
     

function leaveToHandler(id){
 var value=$("#"+id).val();
 if(!verifyData(id,value)){
  var obj=$("#"+id);
  obj.select();
  obj.focus();  
  setTimeout(function(){obj.focus();obj.select();}, 4000);
 }
} 
  
function verifyData(id,data){
 var isSuccess = true;
 var valueStr=data+""; 
 var regu =  /^[-]?[0-9]+(\.[0-9]+)?$/; //验证带符号和小数点的数字
 var patt = new RegExp(regu);
 var lengthData = 7; //数字长度,不包括符号
 var decimalDigits = 2; //小数点后的位数
 
 if(valueStr.length==0){
  $("#"+id).val("0.0");
  return isSuccess;
 }
 
 if(!patt.test(valueStr)){
  showMessage(id,ERROR_MESSAGE.LIMIT_NUMBER);
  isSuccess= false;
  return isSuccess;
 }
 
 var indexStr = valueStr.indexOf('-'); 
//合法取值是 0 到 stringObject.length - 1,如果要检索的字符串值没有出现,则该方法返回 -1
 if(indexStr==0){
  valueStr = valueStr.substr(1); 
//stringObject.substr(start,length),如果没有指定 lenght,那么返回的字符串包含从 start 到 stringObject 的结尾的字符。
 }
 
 if(valueStr.length>lengthData){
  showMessage(id,ERROR_MESSAGE.LIMIT_NUMBER);
  isSuccess= false;
 }else if(valueStr.length<=lengthData){
  var index=valueStr.indexOf(".");
  if(index>0&&(valueStr.length-index>=(decimalDigits+1))){
   showMessage(id,ERROR_MESSAGE.LIMIT_NUMBER);
   isSuccess= false;
  }   
 }
 
 return isSuccess;
}  

 function showMessage(inputId,msg){
  msg="<span id='msg_"+inputId+"' class='hint'>"+msg+"</span>";
  $("#"+inputId).after(msg);
  var msgId="msg_"+inputId;  
  window.setTimeout("hideMessage('" + msgId + "')",4000);
 }
 function hideMessage(msgId){  
  $("#"+msgId).remove();
 }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值