Cash calculator
// click on keyboard
$('.calculator__item:not(#send-money-clear)').on('click', function(e) {
e.stopPropagation();
e.preventDefault();
e.cancelable = true;
var $sendMoney = $('#send-money');
Calculator.plus({
sendMoney: $sendMoney,
sendMoneyVal: $sendMoney.val(),
currentVal: $(this).find('span').text(),
action: $(this).closest('[data-action]').data('action')
});
Calculator.validator({ _this: $sendMoney });
});
$('#send-money').on('keyup', function() {
Calculator.validator({ _this: $(this) });
});
// clear input data
$('#send-money-clear').on('click', function(e) {
e.stopPropagation();
e.preventDefault();
Calculator.clear({ _this: $('#send-money') });
});
var Calculator = (function() {
return {
plus: function(data) {
if(data.sendMoneyVal.match(/[0-9,.]/g) === null) data.sendMoneyVal = '';
if(data.sendMoneyVal) {
data.sendMoneyVal = data.sendMoneyVal.match(/[0-9,.]/g).join("");
}
if(data.action === 'plus') {
data.sendMoney.val(+data.currentVal + (+data.sendMoneyVal));
}
else {
data.sendMoney.val(data.sendMoneyVal + '' + data.currentVal);
}
},
validator: function(data) {
data._this.val(currency + ' ' + data._this.val().match(/[0-9,.]/g).join(""));
},
clear: function(data) {
if(data._this.val().match(/[0-9,.]/g) === null) return;
// data._this.focus();
data._this.val(data._this.val().substring(0, data._this.val().length - 1));
}
}
}());