Regex 3 chars
Regex
by bullrout
HTML
<input type="text" class="creditCardText" />
<button class="btn" type="button">Button</button>
JavaScript
$('.btn').click(function() {
var foo = $('.creditCardText').val().replace(/ /g, '').split("-").join(""); // remove hyphens
var counter=0;
var charLength = foo.length;
Array.prototype.forEach.call(foo, function (chr) {
console.log("counter"+counter);
console.log("charLength"+charLength);
counter++;
console.log(counter);
});
});
//$('.creditCardText').keyup(function() {
// var foo = $(this).val().split("-").join(""); // remove hyphens
// if (foo.length > 0) {
// foo = foo.match(new RegExp('.{1,4}', 'g')).join("-");
// }
// $(this).val(foo);
//});