当前位置: 代码迷 >> Web前端 >> 兼容IE跟FirFox-禁止输入空字符-定位光标
  详细解决方案

兼容IE跟FirFox-禁止输入空字符-定位光标

热度:86   发布时间:2012-11-21 08:23:26.0
兼容IE和FirFox--禁止输入空字符-定位光标
<script type="text/javascript">
var len;
//去掉左边空格
function ltrim(s) 
{ 
 return s.replace(/(^\s*)/g, ''); 
} 

//去掉右边空格
function rtrim(s) 
{ 
return s.replace(/(\s*$)/g, ''); 
} 

//去掉左右空格
function trim(s){ 
return rtrim(ltrim(s));  
} 

function clearNoStr(obj){
          var start=obj.selectionStart;
          var end=obj.selectionEnd;
		  var report=document.getElementById(obj.id).value;
          var reportItem=trim(report);
		  var res=report.substring(start-1,start);
				if(typeof document.selection != "undefined")
                 {
					var s=document.selection.createRange();   
					s.setEndPoint("StartToStart",obj.createTextRange()) 
					len=trim(s.text).length;
                 }else{
				     obj.focus();
					 obj.setSelectionRange(start,end);
				 }
          
           
          if(reportItem=='' || reportItem==null){
          			document.getElementById(obj.id).value=report.substring(report.length);
          }else{
             var count=report.substring(0,report.length-reportItem.length);
             var countLength=count.length;
			 var countStr="";
			 //去掉左边的空格符
             for(var i=0;i<countLength;i++){
                 countStr+=" ";
             }
			 var temp=report.substring(0,1);
			 
             if(count==countStr){
			   document.getElementById(obj.id).value=report.substring(countLength);
			   if(temp==" "){
			      if(typeof document.selection != "undefined")
                  {
				        var range=obj.createTextRange(); 
                        range.moveStart('character',0);
                        range.collapse(true); 
                        range.select();
                   }else{
					    obj.focus();
			            obj.setSelectionRange(0,0);
                   } 
			   }
             }else{
              document.getElementById(obj.id).value=report.substring(0,reportItem.length);
              }
         }
         clearInnerStr(obj,temp,res,start,end);
}
 function clearInnerStr(obj,temp,res,start,end){
        var reqStr=document.getElementById(obj.id).value;
		var size=reqStr.length;
		var noStr="";
		var k=0;
		for(var i=0;i<size;i++){
		   noStr=reqStr.charAt(i);
		    if(noStr==" "){
			   k++;
		       IgnoreSpaces(obj.id,reqStr);
			}
		}
		if(typeof document.selection != "undefined")
        {
				     var range = obj.createTextRange(); 
                     range.moveStart('character',len);
                     range.collapse(true); 
                     range.select();
         }else{

			if(temp==" "){      
			  obj.setSelectionRange(0,0);
			}else{
			     var resStr=document.getElementById(obj.id).value;
				 var resSize=resStr.length;
				  if(res==" "&&k>0){
				    obj.setSelectionRange(start-k,end-k);
				  }
			}
        } 

}

function IgnoreSpaces(id,Str){
    var ResultStr = ""; 
    Temp=Str.split(" ");//双引号之间是一个空格
    for(i = 0; i < Temp.length; i++){ 
    ResultStr +=Temp[i]; 
    }
    document.getElementById(id).value=ResultStr;
}
</script>
禁止在内容中输入空字符:<input type="text" id="name" value="" onkeyup="clearNoStr(this);"><p>