碰到过很多需求:输入数字的时候要求加空格;这些应该是在网上搜的最理想的了
var oT = document.getElementById('bno');
oT.oninput = function(ev) {
$("#submitid").addClass("butHui2");
$("#submitid").removeClass("butRed");
var oW = oT.value;
var waybill = document.getElementById('waybill');
oW = oW.replace(/\D+/g, "");
waybill.value = oW;
oT.value = oW;
var oEvent=ev||event;
if(oEvent.keyCode==8)
{
if(oW)
{
for(var i=0;i<oW.length;i++)
{
var newStr=oW.replace(/\s$/g,'');
}
oT.value=newStr;
}
}else{
var t = oW.split("");
var tt = "";
for (var i = 0; i < oW.length; i++) {
tt += t[i];
if ((i + 1) % 4 == 0 && (i + 1) != oW.length) {
tt += " ";
}
}
oT.value = tt;
}
}
每4位加空格;
<input placeholder="请输入12位运单号" id="bno" maxlength="14" onkeyup="this.value =this.value.replace(/\s/g,'').replace(/[^\d]/g,'').replace(/(\d{4})(?=\d)/g,'$1 ');"/>
数字4位加空格
猜你喜欢
转载自wyr123789.iteye.com/blog/2400069
今日推荐
周排行