自定义html maxlength

function checkSpecialChar(obj,type) {
 var temp="";
 if(type==1){//登录名/姓名在添加和查询时,限制如下字符:
   temp="[`\\-_\\=\\[\\]\\\\;',/~!@#$%^&*()+{}|:\"<>?!@#¥%……&×()·=-{}|~——+【】\;:‘’“”,。、《》?]"; 
 }else if(type==2){//只能数字带小数
  temp="[^0123456789.]";
 }else if(type==3){//配件名称、配件号,自定义配件应允许全角/半角的'-'/ '_'/'.'
  temp="[`\\=\\[\\]\\\\;',/~!@#$%^&*()+{}|:\"<>?!@#¥%……&×()·={}|~+【】\;:‘’“”,。、《》?]"; 
 }else if(type==4){//规则编号只允许输入数字
  temp="[^0123456789]";
 }if(type==5){//事故号/定损单号限制如下字符:
   temp="[`\\=\\[\\]\\\\;',-/~!@#$%^&*()+{}|:\"<>?!@#¥%……&×()·=-{}|~——+【】\;:‘’“”,。、《》?]"; 
 }
 var val = obj.value;
 var patrn = new RegExp(temp);
 if (!val) {
        return true;
 }
 if (val.match(patrn) == null) {
  return true;
 } else {
  obj.value=obj.value.replace(patrn,"");
  return false;
 }
}
var previouslyObjVal ='';
var cursorVal = -1;
var statStr = '';
var statEnd = '';
var currentStrLen = 0;
var copyFlag = false;
function keyUpvalidationStrLength(obj,maxlen,encodingType){
        var str = getClipboard();
        str = str.replaceAll(' ','');
    var encodeType = encodingType == 1?'NVARCHAR2':'VARCHAR2';
     if(null != obj){
      if(maxlen<obj.value.getBytes(encodeType)){
      if(copyFlag){
       var differLen = maxlen - currentStrLen;
       if(currentStrLen==0){differLen = maxlen;};
       var differStr = subStringVal(str,differLen,encodingType);
       obj.value = statStr+differStr+statEnd ; 
      }else{
       obj.value = previouslyObjVal;
      } 
      return false;
       }
     }
 }
 function keydownvalidationStrLength(obj,maxlen,encodingType){
  previouslyObjVal = obj.value;
  cursorVal = getPosition(obj);
  statStr = previouslyObjVal.substr(0,cursorVal);
  statEnd = previouslyObjVal.substr(cursorVal,obj.value.length);
  currentStrLen = previouslyObjVal.getBytes(encodeType);
  var e=e || event;
  var currKey=e.keyCode;
  if(e.ctrlKey == true && currKey == 86 ){copyFlag = true;}
     switch (currKey) {
     case 8:  // allow delete
     case 9:
     case 17:
     case 36: // and cursor keys
     case 35:
     case 37:
     case 38:
     case 39:
     case 40:
     case 46:
     case 65:
         return;
         default:
            var encodeType = encodingType == 1?'NVARCHAR2':'VARCHAR2';
         if(null != obj){
         if(maxlen <= obj.value.getBytes(encodeType)){
          return false;
           }
         }
         return true;
       }
 }
 function subStringVal(val,maxlen,encodingType){
  var encodeType = encodingType == 1?'NVARCHAR2':'VARCHAR2';
  var strVal = "";
  var strlen = 0;
     if(null != val){
         if(encodeType == 'NVARCHAR2'){
          return  val.substring(0,maxlen);
         }
         if(encodeType = 'VARCHAR2'){
          for(var i=0;i<val.length;i++){
           var ov = val.charAt(i);
           var cArr = ov.match(/[^\x00-\xff]/ig);
           if(null == cArr){
            strlen = strlen+1;
           }else{
            strlen = strlen+3;
           }
             if(strlen<=maxlen){
              strVal += ov;
             }else{
              return strVal;
             }
          }
        
         }
     }
 } 
 
  function getClipboard() {
         if (window.clipboardData) {
             return (window.clipboardData.getData('Text'));
         }
         else if (window.netscape) {
             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
             var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
             if (!clip) return;
             var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
             if (!trans) return;
             trans.addDataFlavor('text/unicode');
             clip.getData(trans, clip.kGlobalClipboard);
             var str = new Object();
             var len = new Object();
             try {
                 trans.getTransferData('text/unicode', str, len);
             }
             catch (error) {
                 return null;
             }
             if (str) {
                 if (Components.interfaces.nsISupportsWString) str = str.value.QueryInterface(Components.interfaces.nsISupportsWString);
                 else if (Components.interfaces.nsISupportsString) str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
                 else str = null;
             }
             if (str) {
                 return (str.data.substring(0, len.value / 2));
             }
         }
         return null;
     }

  /**
  * 获取光标所在的字符位置
  * @param obj 要处理的控件, 支持文本域和输入框
  * @author hotleave
  */
  function getPosition(obj){
  //alert(obj.tagName);
  var result = 0;
  if(obj.selectionStart){ //非IE浏览器
     result = obj.selectionStart
  }else{ //IE
     var rng;
     if(obj.tagName == "TEXTAREA"){ //如果是文本域
      rng = event.srcElement.createTextRange();
      rng.moveToPoint(event.x,event.y);
     }else{ //输入框
      rng = document.selection.createRange();
     }
     rng.moveStart("character",-event.srcElement.value.length);
     result = rng.text.length;
  }
     return result;
  }
 
  $(function(){
   jQuery.each($("input"), function(i, o) {
        var verify =  $(o).attr('verify');
        if(null != verify){
          $(o).bind("keydown",function(){
          keydownvalidationStrLength(o,verify);
        });
          $(o).bind("propertychange",function(){
          keyUpvalidationStrLength(o,verify);
        });
        }
      });
  });

你可能感兴趣的:(自定义html maxlength)