Here I am using regex pattern to mask last 4 digits for Credit Card number.
$("#ccnumber").html(ccnbr); //ccnumber refers to div ID
$("#ccnumber").text(function(_, val) {
return val.replace(/\d{12}(\d{4})/, "************$1");
});
It is applicable for only 16 digit credit card numbers. But, I need to mask the incoming numbers (may be 10 or 11) by *
and show the last 4 digits.
Is it possible in javascript/jQuery?
$('#ccnumber').text(function(_, val) {
return val.replace(/\d{12}(\d{4})/, "************$1");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="ccnumber">1234567891234567</div>
replace(/\d(? =\d{4})/g, "*"); to mask all but last 4 digits in a given number of more than 4 digits.
const masked = 'r... [email protected]'; We are required to write a JavaScript function that takes in an email string and returns the masked email for that string.
Just change the mask to 9999 9999 9999 9999 and it will work for your case. You can use dynamic masks if later you want to change the credit card mask based on the value (because some credit cards have different masks).
In C#, MaskedTextBox control gives a validation procedure for the user input on the form like date, phone numbers, etc. Or in other words, it is used to provide a mask which differentiates between proper and improper user input.
You can use:
str = str.replace(/\d(?=\d{4})/g, "*");
to mask all but last 4 digits in a given number of more than 4 digits.
Explanation:
(?=\d{4})
which means match should be followed by 4 digits.\d
matches a single digit with above lookahead condition and replaces that by *
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With