在银行卡号信息输入的时候,为了输入更加清晰,需要对银行卡号进行格式化处理,即每四位添加一个空格。
- JQuery代码如下:
function formatBankCarNumber (BankNo){
if (BankNo.value == "") return;
var account = new String (BankNo.value);
account = account.substring(0,30); /*帐号的总数, 包括空格在内 */
if (account.match (".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null){
/* 对照格式 */
if (account.match (".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" +
".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|" + ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null){
var accountNumeric = "";
var accountChar = "";
var i = undefined;
for (i=0;i<account.length;i++){
accountChar = account.substr (i,1);
if (!isNaN (accountChar.length) && (accountChar != " ")) accountNumeric = accountNumeric + accountChar;
}
account = "";
for (i=0;i<accountNumeric.length;i++){ /* 可将以下空格改为-,效果也不错 */
if (i == 4) account = account + " "; /* 帐号第四位数后加空格 */
if (i == 8) account = account + " "; /* 帐号第八位数后加空格 */
if (i == 12) account = account + " ";/* 帐号第十二位后数后加空格 */
if (i == 16) account = account + " ";/* 帐号第十六位后数后加空格 */
account = account + accountNumeric.substr (i,1)
}
}
} else {
account = " " + account.substring (1,5) + " " + account.substring (6,10) + " " + account.substring (14,18) + "-" + account.substring(18,25);
}
if (account != BankNo.value) {
BankNo.value = account;
}
}
- 使用银行卡格式化方法如下:
<input type="tel" maxlength="25" placeholder="填写银行卡号" onkeyup="formatBankCarNumber(this)" onkeydown="formatBankCarNumber(this)">