So I am trying to get credit card information from a card reader using javascript.
This is what I have:
<script type="text/javascript">
function readCard () {
$("#swiped").focus();
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 2000; //time in ms, 5 second for example
//on keyup, start the countdown
$('#swiped').keyup(function(){
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//on keydown, clear the countdown
$('#swiped').keydown(function(){
clearTimeout(typingTimer);
});
function doneTyping () {
var card_data = document.getElementById('swiped').value;
var details1 = card_data.split("^");
var card_number = details1[0];
card_number = card_number.substring(2);
var names = details1[1].split("/");
var first_name = names[1];
var last_name = names[0];
var details2 = details1[2].split(";");
details2 = details2[1].split("=");
var exp_date = details2[1];
exp_date = exp_date.substring(0, exp_date.length - 1);
exp_date_month = exp_date.substring(2,4);
exp_date_year = exp_date.substring(3, 1);
//document.getElementById("first_name").value = first_name;
//document.getElementById("last_name").value = last_name;
document.getElementById("cardNumber").value = card_number;
document.getElementById("cardExpMonth").value = exp_date_month;
document.getElementById("cardExpYear").value = exp_date_year;
$("#swiped").val('');
$("#swiped").blur();
}
}
</script>
It starts a timer and then when that timer runs out it does the code. Everything is working right. It tears the credit card readers string apart and shows everything right except for the expiration year. So the point I am having issues with is:
var exp_date = details2[1]; exp_date = exp_date.substring(0, exp_date.length - 1); exp_date_month = exp_date.substring(2,4); exp_date_year = exp_date.substring(3, 1);
I got this code from this question http://stackoverflow.com/questions/19018799/parsing-credit-card-data-from-magnetic-stripe-reader-using-javascript
and it works swell except for the darn expiration. I can not figure it out.
It should result in exp month: 10 and exp year being: 15 but instead it results in exp month: 10 and exp year being: 51.
A string from the card reader would look like:
%B4242424242424242^EVANS/MITCH^15101011906300074000000?;4242424242424242=151010119063074?
Where:
4242424242424242
would be the card number
and:
15101011906300074000000
would be the expiration number
And no that isn't real credit card data ;)
Not sure where you come up with exp_date.substring(3, 1)
, it should be exp_date.substring(0, 2)
.
exp_date.substring(3, 1)
is the same than exp_date.substring(1, 3)
which takes from the 2nd to the 4th character, which is why you get 51.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.