Created
June 3, 2011 02:50
-
-
Save benmills/1005778 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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"'); |
This file contains hidden or 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
// 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
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