JS uses regular expressions to control users' input of bank card numbers and formatting. js bank cards

Source: Internet
Author: User

JS uses regular expressions to control users' input of bank card numbers and formatting. js bank cards

// Js Regular Expression for controlling and formatting the user's input bank card number

<Script language = "javascript" type = "text/javascript"> function formatBankNo (BankNo) {if (BankNo. value = "") return; var account = new String (BankNo. value); account = account. substring ();/* Total number of accounts, including Spaces */if (account. match (". [0-9] {4}-[0-9] {4}-[0-9] {4}-[0-9] {7 }") = null) {/* control format */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 ++) {/* You can change the following space to-, and the effect is good */if (I = 4) account = account + ""; /* Add spaces after the fourth digit of the account */if (I = 8) account = account + "";/* Add spaces after the eighth digit of the account */if (I = 12) account = account + "";/* Add a space after the first digit of the account */account = account + accountNumeric. substr (I, 1) }}else {account = "" + account. substring (1, 5) +" "+ Account. substring () +" "+ account. substring () +"-"+ account. substring ();} if (account! = BankNo. value) BankNo. value = account ;}</script>
<input type="text" value="" size="25" onkeyup="formatBankNo(this)" onkeydown="formatBankNo(this)" name="account" id="account">

The above section describes how to use regular expressions to control the user's input and formatting of bank card numbers. I hope this will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.