去掉输入中的空格 但是字符串中的光标位置不变
<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 s=document.selection.createRange();??
?????s.setEndPoint("StartToStart",obj.createTextRange())
?????len=trim(s.text).length;
????????? var report=document.getElementById(obj.id).value;
????????? var reportItem=trim(report);
??
????????? if(reportItem=='' || reportItem==null){
????????? ??????document.getElementById(obj.id).value=report.substring(report.length);
????????????????????? document.getElementById(obj.id).focus();
?????????????????????? return ;
????????? }else{
???????????? var count=report.substring(0,report.length-reportItem.length);
???????????? var countLength=count.length;
??? var countStr="";
??? //去掉左边的空格符
???????????? for(var i=0;i<countLength;i++){
???????????????? countStr+=" ";
???????????? }
???????????? if(count==countStr){
????????????? document.getElementById(obj.id).value=report.substring(countLength);
????
???????????? }else{
????????????? document.getElementById(obj.id).value=report.substring(0,reportItem.length);
????????????? }
????
???????? }
???????? clearInnerStr(obj);
}
?function clearInnerStr(obj){
??????? var reqStr=document.getElementById(obj.id).value;
??var size=reqStr.length;
??var noStr="";
??for(var i=0;i<size;i++){
???? noStr=reqStr.charAt(i);
????? if(noStr==" "){
???????? IgnoreSpaces(obj.id,reqStr);
???}
??}
var range = obj.createTextRange();
range.moveStart('character',len);
range.collapse(true);
range.select();
}
function getPos(obj){??
s=document.selection.createRange();??
s.setEndPoint("StartToStart",obj.createTextRange())??
len=s.text.length;
?
}
function PutCursorAtFirst(obj)
??? {
??????? obj.focus();
??????? var range = obj.createTextRange();
??????? range.moveStart('character',6);
??????? range.collapse(true);
??????? range.select();
??? }
function IgnoreSpaces(id,Str){
??? var ResultStr = "";
??? Temp=Str.split(" ");//双引号之间是一个空格
??? for(i = 0; i < Temp.length; i++){
??? ResultStr +=Temp[i];
??? }
??? document.getElementById(id).value=ResultStr;
??? //document.getElementById(id).focus();
}
function setCursor(input,event) {
???? //alert(String.fromCharCode(event.keyCode));
??? var i=document.getElementById(input.id).value;
??? var start=i.length;
??? var end=i.length;
??? for(var j=0;j<i.length;j++){
??????? //alert(i.charAt(j));
??? }
??? /*
??? if(input.setSelectionRange) {?
????????? input.focus();?
???????? input.setSelectionRange(start,end);?
?????? }?
??? else {
??? */?
?????? if(input.createTextRange) {?
????????? range=input.createTextRange();?
????????? range.collapse(true);?
????????? range.moveEnd('character',end);?
???????? range.moveStart('character',start);?
???????? range.select();?
?????? }?
??? /*
??? }
??? */
?}?
</script>
禁止在内容中输入空字符:<input type="text" id="name" value="" onkeyup="clearNoStr(this);"><p>
<input type="text" id="name2" value="" onkeyup="setCursor(this,event);">