Reputation: 443
I'm working on a field that convert some part of the string to asterisk.. the first four and last four is not converted only the other characters. so when I reach the fifth character it will be converted and when it reach the 10th character it will be normal again, the string length is 13. My problem with my code is I cant get that kind of process.
Hope you help me.
Thanks.
function asteriskCard(string) {
var lastfour = string.substr(string.length - 4);
var firstfour = string.substr(0, 4);
return firstfour + ' ***** ' + lastfour;
}
$('input').keyup(function(){
$('div').text(asteriskCard($(this).val()))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
<div></div>
Upvotes: 0
Views: 1258
Reputation: 27051
Try this, I think its what your looking for.
function asteriskCard(string) {
var lastfour = string.length > 8 ? string.substr(string.length - (string.length - 9)) : "";
var firstfour = string.substr(0, 4);
var dots = '*****'.substring(0,string.length - 4);
var total = (firstfour) + (dots.length > 0 ? ' ' + dots + ' ' : dots) + (lastfour);
return total;
}
$('input').keyup(function() {
$('div').text(asteriskCard($(this).val()))
});
demo
function asteriskCard(string) {
var lastfour = string.length > 8 ? string.substr(string.length - (string.length - 9)) : "";
var firstfour = string.substr(0, 4);
var dots = '*****'.substring(0,string.length - 4);
var total = (firstfour) + (dots.length > 0 ? ' ' + dots + ' ' : dots) + (lastfour);
return total;
}
$('input').keyup(function() {
$('div').text(asteriskCard($(this).val()))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" maxlength="13">
<div></div>
Upvotes: 2