如何自动加空格,并删除空格的方法??根据下面的提示,希望能帮助您!come on!
var pre=0,//上一次输入字符个数
next=0,//下一次输入字符个数
inputing;//此次操作是输入还是删除
//控制vin长度
function test(){
// var pre=document.getElementById('vin').value.length;
var next=document.getElementById('vin').value.length;
console.log(pre+"======="+next);
if(next>pre){
inputing = true;console.log("正在输入");
}else if(next<=pre){
inputing = false;console.log("正在删除");
}
pre = next;//交换
var len=document.getElementById('vin').value.length;
var reg = /\s{1,}/g;
var vin_ = "";
var vin=document.getElementById('vin').value;
//循环
//for(var i = 0;i < len;i++){
if(len == 4||len == 9 ||len == 14||len == 19)
{
if(inputing){//删除不要进入这里;
vin_ = vin_ + vin + " ";
}else{
vin_ = vin_ + vin;
}
}else{
vin_ = vin_ + vin;
}
//}
vin = vin.replace(reg,"");//取消空格
document.getElementById("vin").value = vin_;
document.getElementById('vin1').value =vin.toUpperCase();//小写字母输入转为大写字母传入后台
return vin;
}
//控制cellphoneNumber长度
/* window.οnlοad=function(){
var t=document.getElementById("cellphoneNumber");
//t.οnkeydοwn=test1;
t.οnkeyup=test1;
//t.οnkeypress=test1;
}; */
function test1(){
var next=document.getElementById('cellphoneNumber').value.length;
console.log(pre+"======="+next);
if(next>pre){
inputing = true;console.log("正在输入");
}else if(next<=pre){
inputing = false;console.log("正在删除");
}
pre = next;//交换
var len=document.getElementById('cellphoneNumber').value.length;
var reg = /\s{1,}/g;
var cellphoneNumber_ = "";
var cellphoneNumber=document.getElementById('cellphoneNumber').value;
/* for(var i = 0;i < len;i++){ */
if(len == 3||len == 8||len == 13)
{
if(inputing){//删除不要进入这里;
cellphoneNumber_ = cellphoneNumber_ + cellphoneNumber + " ";
}else{
cellphoneNumber_ = cellphoneNumber_ + cellphoneNumber;
}
}else{
cellphoneNumber_ = cellphoneNumber_ + cellphoneNumber;
}
cellphoneNumber = cellphoneNumber.replace(reg,"");//去除空格
document.getElementById("cellphoneNumber").value = cellphoneNumber_;
document.getElementById("cellphoneNumber1").value = cellphoneNumber;//去空格传入隐藏域
return cellphoneNumber;
}
//控制身份证的空格!
/* window.οnlοad=function(){
var t=document.getElementById("identifyNumber");
//t.οnkeydοwn=test3;
t.οnkeyup=test3;
//t.οnkeypress=test3;
}; */
function test3(){
/* this.value=this.value.replace(/(^\s*)|(\s*$)/g,'');//添加空格
var idd=this.value;
document.getElementById("identifyNumber").value=idd;
idd = idd.replace(/\s+/g,"");//去除空格
document.getElementById('identifyNumber1').value=idd; */
var next=document.getElementById('identifyNumber').value.length;
console.log(pre+"======="+next);
if(next>pre){
inputing = true;console.log("正在输入");
}else if(next<=pre){
inputing = false;console.log("正在删除");
}
pre = next;//交换
var len=document.getElementById('identifyNumber').value.length;
var reg = /\s{1,}/g;
var identifyNumber_ = "";
var identifyNumber=document.getElementById('identifyNumber').value;//增加空格后
//循环
/* for(var i = 0;i < len;i++){ */
if(len == 4||len == 9||len == 14||len == 19)
{
if(inputing){//删除不要进入这里;
identifyNumber_ = identifyNumber_ + identifyNumber + " ";
}else{
identifyNumber_ = identifyNumber_ + identifyNumber;
}
}else{
identifyNumber_ = identifyNumber_ + identifyNumber;
}
identifyNumber = identifyNumber.replace(reg,"");//取消空格
document.getElementById("identifyNumber").value = identifyNumber_;//小写带空格
document.getElementById('identifyNumber1').value =identifyNumber.toUpperCase();//小写字母输入转为大写字母传入后台
return identifyNumber;
}
我是您们的朋友三哥,大龙!