使用javascript仅显示银行帐户中的最后4位数字 [英] show only last 4 digits in bank account using javascript
问题描述
我需要有关Javascript的帮助。我需要替换包含银行帐号的文本字段的最后4位之前的许多字符。我在网上搜索了这个,但找不到一个有效的代码。我确实在stackoverflow上找到了一个关于信用卡的代码,
I need help with Javascript. I need to replace however many characters there are previous to the last 4 digits of a text field that contains bank account number. I have searched through the net on this, but cannot find one code that works. I did find a code here on stackoverflow, which was regarding credit card,
new String('x', creditCard.Length - 4) + creditCard.Substring(creditCard.Length - 4);
我刚用accounNumObject替换了creditCard:
I just replaced the creditCard with accounNumObject:
var accounNumObject = document.getElementById("bankAcctNum")
输入非常简单。
<cfinput type="text" name="bankAcctNum" id="bankAcctNum" maxlength="25" size="25" value="#value#" onblur="hideAccountNum();">
有人可以帮忙吗?
推荐答案
要用 x
替换字符串,除了JavaScript中的最后四个字符,你可以使用(假设 str
持有字符串)...
To replace a string with x
except for the last four characters in JavaScript, you could use (assuming str
holds the string)...
var trailingCharsIntactCount = 4;
str = new Array(str.length - trailingCharsIntactCount + 1).join('x')
+ str.slice(-trailingCharsIntactCount);
jsFiddle 。
你也可以使用正则表达式......
You could also use a regular expression...
str = str.replace(/.(?=.{4})/g, 'x');
如果你想添加 4
来自变量,使用 RegExp
构造函数构造正则表达式。
If you want to add the 4
from a variable, construct the regex with the RegExp
constructor.
jsFiddle 。
如果你有幸获得支持,也......
If you're fortunate enough to have the support, also...
const trailingCharsIntactCount = 4;
str = 'x'.repeat(str.length - trailingCharsIntactCount)
+ str.slice(-trailingCharsIntactCount);
的Polyfill> String.prototype.repeat()
可用。
这篇关于使用javascript仅显示银行帐户中的最后4位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!