首页 > 代码库 > js常见input校验

js常见input校验

//校验输入价格等,保留2位小数
function clearNoNum(obj){
    obj.onkeyup = function(event){
      var e = event || window.event;
      if (e.keyCode == 37 || e.keyCode == 39) {//允许输入左右方向键
        return;
      }
      if(((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) {
          return;
      }
      obj.value = obj.value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符   
      obj.value = http://www.mamicode.com/obj.value.replace(/.{2,}/g,"."); //只保留第一个. 清除多余的   
      obj.value = http://www.mamicode.com/obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");  
      obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,‘$1$2.$3‘);//只能输入两个小数   
      if(obj.value.indexOf(".")< 0 && obj.value !=""){//以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额  
       obj.value=http://www.mamicode.com/ parseFloat(obj.value);  
      }  
        
    };
}

//校验输入整数不包括负整数
function clearNoInteger(obj){
    obj.onkeyup = function(event){
          var e = event || window.event;
          if (e.keyCode == 37 || e.keyCode == 39) {//允许输入左右方向键
            return;
          }
          if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) {
              obj.value = obj.value.replace(/[^\d]/g,"");//清除“数字”和“.”以外的字符 
              var numarr = obj.value.split(‘‘);
              if (numarr[0] == 0) {
                  var j = 0;
                  for (var i=0;i <numarr.length; i++) {
                      if (numarr[i] != 0) {
                          j = i;
                          break;
                      }
                      
                  }
                  obj.value = obj.value.substring(j,obj.value.length)
              }
              
          }
          
          if (((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) {
              var numarr = obj.value.split(‘‘);
              if (numarr[0] == 0) {
                  var j = 0;
                  for (var i=0;i <numarr.length; i++) {
                      if (numarr[i] != 0) {
                          j = i;
                          break;
                      }
                      
                  }
                  obj.value = obj.value.substring(j,obj.value.length)
              }    
          }
   };
}

function isPositiveNum(s){//是否为正整数  
    var re = /^[0-9]*[1-9][0-9]*$/ ;  
    return re.test(s)  
}

function validate(num){//是否为正数
  var reg = /^\d+(?=\.{0,1}\d+$|$)/
  if(reg.test(num)) return true;
  return false ;  
}

function stripscript(s) {//过滤特殊字符
    var pattern = new RegExp("[`~!@#$^&*()=|{}‘:;‘,\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“‘。,、?]")
    var rs = "";
    for (var i = 0; i < s.length; i++) {
        rs = rs + s.substr(i, 1).replace(pattern, ‘‘);
    }
    return rs;
}

function containSpecial(s) { //判断是否包含非法字符      
    var containSpecial = RegExp(/[(\ )(\~)(\!)(\@)(\¥)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\‘)(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/);      
    return (containSpecial.test(s));      
}

function getByteLen(val) {//计算字符长度(中文算2个字符,英文算1个字符)
    var len = 0;
    for (var i = 0; i < val.length; i++) {
         var a = val.charAt(i);
         if (a.match(/[^\x00-\xff]/ig) != null) 
        {
            len += 2;
        }
        else
        {
            len += 1;
        }
    }
    return len;
}

//生成对应随机数
function GetRandomNum(Min,Max,type){   
    var Range = Max - Min;
    var Rand = Math.random();
    var num = (Min + Math.round(Rand * Range));
    if(type = 2){
        num = parseFloat(Math.random().toFixed(1)) + parseFloat(num);
    }else if(type == 3){
        num = parseFloat(Math.random().toFixed(2)) + parseFloat(num);
    }
    return num;   
}

//去除前后空格
function trim(s){
    return s.replace(/(^\s*)|(\s*$)/g, "");
}

//获取随机数
function getRandom() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

//生成随机数id
function getId() {
   return (getRandom()+getRandom()+"-"+getRandom()+"-"+getRandom()+"-"+getRandom()+"-"+getRandom()+getRandom()+getRandom());
}

//对Date的扩展,将 Date 转化为指定格式的String
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
//年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
//例子: 
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
//(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
 var o = {
     "M+": this.getMonth() + 1, //月份 
     "d+": this.getDate(), //
     "h+": this.getHours(), //小时 
     "m+": this.getMinutes(), //
     "s+": this.getSeconds(), //
     "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
     "S": this.getMilliseconds() //毫秒 
 };
 if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
 for (var k in o)
 if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
 return fmt;
}

 

js常见input校验