Skip to content

Instantly share code, notes, and snippets.

@AlexRiina
Forked from benmills/btcryptform.jquery.js
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexRiina/4852e1ac0979d71169b5 to your computer and use it in GitHub Desktop.
Save AlexRiina/4852e1ac0979d71169b5 to your computer and use it in GitHub Desktop.
// jQuery Plugin (Change Type)
(function ($) {
$.fn.cryptForm = function (key, toCryptSel) {
var form = $(this);
var braintree = Braintree.create(key);
form.submit(function () {
form.find(toCryptSel).each(function () {
this.type = 'password';
$(this).val(braintree.encrypt($(this).val()));
});
});
};
})(jQuery);
// jQuery Plugin (Clone Form)
(function ($) {
$.fn.cryptForm = function (key, toCryptSel) {
var form = $(this);
var braintree = Braintree.create(key);
form.submit(function (e) {
e.preventDefault();
var new_form = form.clone();
new_form.find(toCryptSel).each(function () {
$(this).val(braintree.encrypt($(this).val()));
});
new_form.hide().appendTo('body').submit().remove();
});
};
})(jQuery);
// Examples
//$("#f").cryptForm(clientSideEncryptionKey, '#cc, #cvv');
//$("#f").cryptForm(clientSideEncryptionKey, '.crypt');
//$("#f").cryptForm(clientSideEncryptionKey, '[name="cc"], [name="cvv"');
// Pure JS (Change Type)
var cryptForm = function (key, formId, toCryptNames) {
var braintree = Braintree.create(key);
document.getElementById(formId).onsubmit = function () {
var els = this.getElementsByTagName("input");
for (i in els) if (toCryptNames.indexOf(els[i].name) > -1) {
els[i].type = 'password';
els[i].value = braintree.encrypt(els[i].value);
}
};
};
// Pure JS (Clone Form )
var cryptForm = function (key, formId, toCryptNames) {
var braintree = Braintree.create(key);
document.getElementById(formId).onsubmit = function () {
var clonedForm = this.cloneNode(true);
var els = clonedForm.getElementsByTagName("input");
for (i in els) if (toCryptNames.indexOf(els[i].name) > -1)
els[i].value = braintree.encrypt(els[i].value);
clonedForm.style.display = 'none';
document.body.appendChild(clonedForm);
clonedForm.submit();
return false;
};
};
// Examples
//cryptForm(clientSideEncryptionKey, 'f', ['cc', 'cvv']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment