Skip to content

Instantly share code, notes, and snippets.

@benmills
Created June 3, 2011 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benmills/1005778 to your computer and use it in GitHub Desktop.
Save benmills/1005778 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();
form.clone().find(toCryptSel).each(function () {
$(this).val(braintree.encrypt($(this).val()));
}).hide().submit();
});
};
})(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']);
@AlexRiina
Copy link

https://gist.github.com/benmills/1005778#file-btcryptform-jquery-js-L26

I'm not very experienced with javascript so I may have overlooked something, but had to do appendTo("body") to get this gist to work in Firefox (http://stackoverflow.com/questions/7117084/jquery-form-submit-on-chrome-works-but-not-in-firefox)

I added a remove() for the hell of it and the full change is https://gist.github.com/AlexRiina/4852e1ac0979d71169b5/revisions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment