Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cbetta/81e1311f9d2c21b91a9b to your computer and use it in GitHub Desktop.
Save cbetta/81e1311f9d2c21b91a9b to your computer and use it in GitHub Desktop.
Braintree Hosted Fields - Card validation

Braintree Hosted Fields - Card validation

In this demo I tried to recreate the Braintree v.zero dropin experience of automatically detecting the credit card validity using the Hosted Field SDK.

A Pen by Cristiano Betta on CodePen.

License.

<form id="example-form">
<div id='card-number'>
</div>
</form>
<!--
These are valid numbers:
* 5555555555554444
* 4111111111111111
These are invalid numbers:
* 5555555555554445
* 4111111111111112
-->
// This clientToken would normally be generated on the server
clientToken = 'eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiI5NmRlNmMyNDJkZTgwNzU0YzNiOTg5MmQwNzlhNWJlYTI5ZTE0MmQ0MjlhZjA0MTQ5N2M0YzUyZmU4MTAyYmQ4fGNyZWF0ZWRfYXQ9MjAxNS0wNy0wM1QxNDozMjo0OS43ODMzNzEwODcrMDAwMFx1MDAyNm1lcmNoYW50X2lkPWRjcHNweTJicndkanIzcW5cdTAwMjZwdWJsaWNfa2V5PTl3d3J6cWszdnIzdDRuYzgiLCJjb25maWdVcmwiOiJodHRwczovL2FwaS5zYW5kYm94LmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvZGNwc3B5MmJyd2RqcjNxbi9jbGllbnRfYXBpL3YxL2NvbmZpZ3VyYXRpb24iLCJjaGFsbGVuZ2VzIjpbXSwiZW52aXJvbm1lbnQiOiJzYW5kYm94IiwiY2xpZW50QXBpVXJsIjoiaHR0cHM6Ly9hcGkuc2FuZGJveC5icmFpbnRyZWVnYXRld2F5LmNvbTo0NDMvbWVyY2hhbnRzL2RjcHNweTJicndkanIzcW4vY2xpZW50X2FwaSIsImFzc2V0c1VybCI6Imh0dHBzOi8vYXNzZXRzLmJyYWludHJlZWdhdGV3YXkuY29tIiwiYXV0aFVybCI6Imh0dHBzOi8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YTA1MjdmNGQ1YjQ0NmQ0ZjY4NDgxN2NiNjIzZDI1NWI1NzNhZGRjNTliIiwibWVyY2hhbnRBY2NvdW50IjoiY29pbmJhc2UtZGV2ZWxvcG1lbnQtbWVyY2hhbnRAZ2V0YnJhaW50cmVlLmNvbSIsInNjb3BlcyI6ImF1dGhvcml6YXRpb25zOmJyYWludHJlZSB1c2VyIiwicmVkaXJlY3RVcmwiOiJodHRwczovL2Fzc2V0cy5icmFpbnRyZWVnYXRld2F5LmNvbS9jb2luYmFzZS9vYXV0aC9yZWRpcmVjdC1sYW5kaW5nLmh0bWwiLCJlbnZpcm9ubWVudCI6Im1vY2sifSwibWVyY2hhbnRJZCI6ImRjcHNweTJicndkanIzcW4iLCJ2ZW5tbyI6Im9mZmxpbmUiLCJhcHBsZVBheSI6eyJzdGF0dXMiOiJtb2NrIiwiY291bnRyeUNvZGUiOiJVUyIsImN1cnJlbmN5Q29kZSI6IlVTRCIsIm1lcmNoYW50SWRlbnRpZmllciI6Im1lcmNoYW50LmNvbS5icmFpbnRyZWVwYXltZW50cy5zYW5kYm94LkJyYWludHJlZS1EZW1vIiwic3VwcG9ydGVkTmV0d29ya3MiOlsidmlzYSIsIm1hc3RlcmNhcmQiLCJhbWV4Il19fQ=='
// This code ties Braitree Hosted Fields to your form
braintree.setup(clientToken, "custom", {
id: "example-form",
hostedFields: {
number: {
selector: "#card-number",
placeholder: "Card Number"
},
styles: {
'.invalid': {
'color': '#D0041D'
}
}
},
});
<script src="https://js.braintreegateway.com/js/beta/braintree-hosted-fields-beta.17.js"></script>
/* Let's add some code to make the form stand out */
form {
width: 300px;
position: relative;
left: 50%;
margin-left: -150px;
background-color: #eee;
padding: 1em;
border-radius: 1em;
margin-top: 2em;
}
#card-number {
position: relative;
height: 2em;
border: 1px solid #ddd;
padding: 0 0.5em;
background-color: white;
}
#card-number.braintree-hosted-fields-invalid {
border-bottom: 2px solid #D0041D;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment