简体   繁体   English

使用javascript只显示银行账户的最后4位数字

[英]show only last 4 digits in bank account using javascript

I need help with Javascript.我需要 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.我需要替换包含银行帐号的文本字段的最后 4 位数字之前的许多字符。 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,我确实在stackoverflow上找到了一个关于信用卡的代码,

new String('x', creditCard.Length - 4) + creditCard.Substring(creditCard.Length - 4);

I just replaced the creditCard with accounNumObject:我只是用 accounNumObject 替换了信用卡:

var accounNumObject = document.getElementById("bankAcctNum")

The input is pretty simple.输入非常简单。

<cfinput type="text" name="bankAcctNum" id="bankAcctNum" maxlength="25" size="25" value="#value#" onblur="hideAccountNum();">

Can anyone help please?有人可以帮忙吗?

To replace a string with x except for the last four characters in JavaScript, you could use (assuming str holds the string)...除了 JavaScript 中的最后四个字符之外,要用x替换字符串,您可以使用(假设str保存字符串)...

var trailingCharsIntactCount = 4;

str = new Array(str.length - trailingCharsIntactCount + 1).join('x')
       + str.slice(-trailingCharsIntactCount);

jsFiddle . js小提琴

You could also use a regular expression...您也可以使用正则表达式...

str = str.replace(/.(?=.{4})/g, 'x');

If you want to add the 4 from a variable, construct the regex with the RegExp constructor.如果要从变量中添加4 ,请使用RegExp构造函数构造正则RegExp

jsFiddle . js小提琴

If you're fortunate enough to have the support, also...如果你有幸得到支持,也...

const trailingCharsIntactCount = 4;

str = 'x'.repeat(str.length - trailingCharsIntactCount)
        + str.slice(-trailingCharsIntactCount);

Polyfill for String.prototype.repeat() is available . String.prototype.repeat() Polyfill 可用

Here is a fiddle showing what you're asking for:这是一个小提琴,显示了您的要求:

http://jsfiddle.net/eGFqM/1/ http://jsfiddle.net/eGFqM/1/

<input id='account' value='abcdefghijklmnop'/>
<br/>
<input id='account_changed'/>
var account = document.getElementById('account');
var changed = document.getElementById('account_changed');

changed.value = new Array(account.value.length-3).join('x') + 
    account.value.substr(account.value.length-4, 4);

Edit: Updated fiddle to correct off by one problem pointed out by alex编辑:更新小提琴以纠正亚历克斯指出的一个问题

Agreed with all above solutions.I just had one another approach.同意上述所有解决方案。我只是有另一种方法。

 const maskAccountId = (accountId) => { if (accountId) { /** Condition will only executes if accountId is *not* undefined, null, empty, false or 0*/ const accountIdlength = accountId.length; const maskedLength = accountIdlength - 4; /** Modify the length as per your wish */ let newString = accountId; for (let i = 0; i < accountIdlength; i++) { if (i < maskedLength) { newString = newString.replace(accountId[i], '*'); } } return newString; } else return /**Will handle if no string is passed */ } console.log(maskAccountId('egrgrgry565yffvfdfdfdfdfgrtrt4t4'));

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

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