script language="javascript" type="text/javascript">
function formatBankNo (BankNo){
if (BankNo.value == "") return;
var account = new String (BankNo.value);
account = account.substring(0,22); /*帳號的總數(shù), 包括空格在內(nèi) */
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 = accountChar = "", i;
for (i=0;iaccount.length;i++){
accountChar = account.substr (i,1);
if (!isNaN (accountChar) (accountChar != " ")) accountNumeric = accountNumeric + accountChar;
}
account = "";
for (i=0;iaccountNumeric.length;i++){ /* 可將以下空格改為-,效果也不錯 */
if (i == 4) account = account + " "; /* 帳號第四位數(shù)后加空格 */
if (i == 8) account = account + " "; /* 帳號第八位數(shù)后加空格 */
if (i == 12) account = account + " ";/* 帳號第十二位后數(shù)后加空格 */
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;
}
/script>
以上所述是小編給大家介紹的JS使用正則控制用戶輸入銀行卡號及格式化,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!