js正則實現使用者輸入銀行卡號的控制及格式化

來源:互聯網
上載者:User

標籤:style   blog   color   io   java   ar   for   div   sp   

//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正則實現使用者輸入銀行卡號的控制及格式化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.