Last active
August 29, 2015 14:00
-
-
Save klzns/11370904 to your computer and use it in GitHub Desktop.
Bar code scanner and credit card reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Bar code scanner and credit card reader</h1> | |
<p id="barcode" style="display:none;"><strong>Bar code:</strong> <span></span></p> | |
<div id="card" style="display:none;"> | |
<h3>Card</h3> | |
<p><strong>Number</strong></p> | |
<p id="card-number"></p> | |
<p><strong>Due Date</strong></p> | |
<p><span id="card-month"></span>/<span id="card-year"></span></p> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isTypingBarcode = false | |
var barcode = '' | |
$(document).on('keydown.barcode', function(e) { | |
var value = String.fromCharCode(e.keyCode) | |
if (/\d/.test(value)) { | |
barcode += value | |
isTypingBarcode = true | |
} | |
else { | |
if (isTypingBarcode && barcode.length === 13 && /\t/.test(value)) { | |
$('#barcode span').text(barcode) | |
$('#barcode').show() | |
} else { | |
isTypingBarcode = false; | |
barcode = '' | |
} | |
} | |
}) | |
var isTypingCard = false; | |
var cardInfo = ''; | |
$(document).on('keydown.card', function(e) { | |
var value = String.fromCharCode(e.keyCode) | |
if (/º/.test(value)) { | |
isTypingCard = true | |
} | |
if (!isTypingCard) { | |
cardInfo = '' | |
return | |
} else { | |
cardInfo += value | |
if (/¿/.test(value)) { | |
cardInfo = cardInfo.replace(/º|¿/g, '').split('»') | |
var cardNumber = cardInfo[0] | |
var cardMonth = cardInfo[1].substr(2,2) | |
var cardYear = cardInfo[1].substr(0,2) | |
$('#card-number').text(cardNumber) | |
$('#card-month').text(cardMonth) | |
$('#card-year').text(cardYear) | |
$('#card').show() | |
cardInfo = '' | |
isTypingCard = false | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Bar code scanner and card reader | |
description: Listen to inputs | |
authors: | |
- Breno Calazans | |
normalize_css: no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment