-2
<td>
<input type="text" id= "test" name="test" size="22" maxlength="22" value="" onkeypress="onsplkeypressTest()" >
</td>
function onsplkeypressTest() {
var _newVal = document.getElementById("test").value;
_newVal = _newVal.split(' ').join('');
var len = _newVal.length+4;
var ind = 4;
while (ind < len){
_newVal = _newVal.substring(0, ind) + ' ' + _newVal.substring(ind, len); //add space
ind = ind + 5;
}
document.getElementById("test").value = _newVal;
}
這使輸出ABCD IJKL NOPQ
但是如果我想在ABCD
和IJKL
之間進入EFGH
它不會允許 它 - 它在最後被追加:ABCD IJKL NOPQ EFGH
onkeypress事件4字符後添加空格
應該寫什麼代碼才能輸出ABCD EFGH IJKL NOPQ
?
你在尋找什麼來格式化信用卡號碼? – naomik
沒有我的要求是既可以在單一的文本框中輸入文字像第四個字符後,這個 安其按自動空間應該得到補充 ABCD EFGH IJKL MNOP 和用戶可以從任何位置 編輯或有四個文本框 ,如果用戶複製完整的16位數字並粘貼到第一個文本框中 剩餘文本應該添加到第二,第三和第四個文本框中 –