最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
JS使用正则控制用户输入银行卡号或者格式化
时间:2022-06-29 01:33:02 编辑:袖梨 来源:一聚教程网
//js正则实现用户输入银行卡号的控制及格式化
functionformatBankNo (BankNo){
if(BankNo.value =="")return;
varaccount =newString (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){
varaccountNumeric = accountChar ="", i;
for(i=0;i accountChar = account.substr (i,1); if(!isNaN (accountChar) && (accountChar !=" ")) accountNumeric = accountNumeric + accountChar; } account =""; for(i=0;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; } > 以上所述是小编给大家介绍的JS使用正则控制用户输入银行卡号及格式化,希望对大家有所帮助。相关文章