JS使用正则控制用户输入银行卡号及格式化
//js正则实现用户输入银行卡号的控制及格式化
<script language="javascript" type="text/javascript"> function formatBankNo (BankNo){ if (BankNo.value == "") return; var account = new String (BankNo.value); account = account.substring(0,22); /*帐号的总数, 包括空格在内 */ 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;i<account.length;i++){ accountChar = account.substr (i,1); if (!isNaN (accountChar) && (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 + " ";/* 帐号第十二位后数后加空格 */ 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>
<input type="text" value="" size="25" onkeyup="formatBankNo(this)" onkeydown="formatBankNo(this)" name="account" id="account">
以上所述是小编给大家介绍的JS使用正则控制用户输入银行卡号及格式化,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对安科网网站的支持!
相关推荐
梦的天空 2020-08-25
天高任鸟飞 2020-06-13
Darklovy 2020-06-11
杨德龙 2020-11-11
不要皱眉 2020-10-14
满地星辰 2020-09-16
lrjnlp 2020-07-19
qidu 2020-07-05
flyingssky 2020-07-05
flyingssky 2020-06-27
RuoShangM 2020-06-17
qidu 2020-06-08
Darklovy 2020-06-07
jyj00 2020-06-06
flyingssky 2020-06-04
山水沐光 2020-05-26
山水沐光 2020-05-25