简体   繁体   English

JavaScript格式化帐号

[英]JavaScript to format account number

I have to format an account number input field like below: 我必须格式化一个帐号输入字段,如下所示:

Desired format: 123456 1234 12 1 所需格式: 123456 1234 12 1

在此处输入图片说明

But, the regex which I wrote formats the text field as below: 但是,我编写的正则表达式将文本字段的格式设置如下:

Current format: 1234 1234 1234 1 当前格式: 1234 1234 1234 1

Can someone please help me with the correct regex or logic to implement this? 有人可以通过正确的正则表达式或逻辑来帮助我吗?

 function cc_format(value) { var v = value.replace(/\\s+/g, '').replace(/[^0-9]/gi, '') var matches = v.match(/\\d{4,13}/g); var match = matches && matches[0] || '' var parts = [] for (i = 0, len = match.length; i < len; i += 4) { parts.push(match.substring(i, i + 4)) } if (parts.length) { return parts.join(' ') } else { return value } } onload = function() { document.getElementById('account-number').oninput = function() { this.value = cc_format(this.value) } } 
 <form> <div>AccountNumber</div><br/> <input id="account-number" value="" placeholder="123456 1234 12 1"> </form> 

You can get groups of 6, instead of groups of 4, then, as soon as your second group gets over 4 characters long, you can insert a space at the desired position. 您可以得到6个组,而不是4个组,然后,只要第二个组的长度超过4个字符,就可以在所需位置插入一个空格。 Here's a proof of concept: 这是概念证明:

 function cc_format(value) { var v = value.replace(/\\s+/g, '').replace(/[^0-9]/gi, '') var matches = v.match(/\\d{6,13}/g); var match = matches && matches[0] || '' var parts = [] for (i = 0, len = match.length; i < len; i += 6) { parts.push(match.substring(i, i + 6)) } if (parts.length > 1 && parts[1].length > 4) { parts[1] = [parts[1].slice(0, 4), ' ', parts[1].slice(4)].join(''); } if (parts.length) { return parts.join(' ') } else { return v } } onload = function() { document.getElementById('account-number').oninput = function() { this.value = cc_format(this.value) } } 
 <form> <div>AccountNumber</div><br/> <input id="account-number" value="" placeholder="123456 1234 12 1"> </form> 

A slightly more elegant solution would be to use .slice() and .join() for the whole thing, which cleans up the code nicely. 稍微更优雅的解决方案是在整个过程中使用.slice().join() ,从而很好地清理了代码。 Here's an example: 这是一个例子:

 function cc_format(value) { var v = value.replace(/\\s+/g, '').replace(/[^0-9]/gi, ''); if (v.length > 6) v = [v.slice(0, 6), ' ', v.slice(6)].join(''); if (v.length > 11) v = [v.slice(0, 11), ' ', v.slice(11)].join(''); if (v.length > 14) v = [v.slice(0, 14), ' ', v.slice(14)].join(''); if (v.length > 16) v = v.slice(0, 16); return v; } onload = function() { document.getElementById('account-number').oninput = function() { this.value = cc_format(this.value) } } 
 <form> <div>AccountNumber</div><br/> <input id="account-number" value="" placeholder="123456 1234 12 1"> </form> 

You can use following code, to format the code properly when user is typing. 您可以使用以下代码来在用户键入时正确设置代码格式。 It works dynamically. 它动态地工作。

 const normalize = elem => { let value = elem.target.value; const onlyNums = value.replace(/[^\\d]/g, ''); if (onlyNums.length <= 6) { return onlyNums; } else if (onlyNums.length <= 10) { return `${onlyNums.slice(0, 6)} ${onlyNums.slice(6)}`; } else if (onlyNums.length <= 12) { return `${onlyNums.slice(0, 6)} ${onlyNums.slice(6, 10)} ${onlyNums.slice(10, 12)}`; } return `${onlyNums.slice(0, 6)} ${onlyNums.slice(6, 10)} ${onlyNums.slice(10, 12)} ${onlyNums.slice(12, 13)}`; }; const input = document.getElementById('input'); input.addEventListener('input', (e) => e.target.value = normalize(e)); 
 <input type='text' id='input' value=''> 

How about using Substring? 使用子串怎么样?

var first = this.value.substring(0, 6)
var second = this.value.substring(6, 10)
var third = this.value.substring(10, 12)
var fourth = this.value.substring(12, 13)

JsFiddle example: https://jsfiddle.net/c9p70fw1/ JsFiddle示例: https ://jsfiddle.net/c9p70fw1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM