Skip to content

Instantly share code, notes, and snippets.

@salsalabs
Last active April 21, 2017 19:54
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 salsalabs/91020a16b6cd99cbcc6a to your computer and use it in GitHub Desktop.
Save salsalabs/91020a16b6cd99cbcc6a to your computer and use it in GitHub Desktop.
Script to capitalize all workds in a Salsa form before the form is submitted.
// Generated by LiveScript 1.5.0
// https://gist.github.com/salsalabs/add8c513630be2c5f95abef383cd856a
var initCap, capitalize, blurHandler, getInputs, notUsed, unwantedTypes, skip, run, actor, interval;
initCap = function(t){
switch (t) {
case null:
return t;
default:
return t.slice(0, 1).toUpperCase() + t.slice(1).toLowerCase();
}
};
capitalize = function(t){
return t.split(' ').map(initCap).join(' ');
};
blurHandler = function(event){
return event.target.value = capitalize(event.target.value);
};
getInputs = function(){
// SalsaStaff 175757: This doesn't work in IE. Replaced with something that does.
// return Array.from(document.querySelectorAll('input')).filter(skip);
return [].map.call(document.querySelectorAll('input'), function(e) { return e; }).filter(skip);
};
notUsed = ['cc', 'ccExpMonth', 'ccExpYear', 'checkingAccountNumber', 'checkingRoutingNumber', 'Email', 'CVV2', 'amountOther'];
unwantedTypes = ['hidden', 'radio', 'checkbox', 'submit'];
skip = function(e){
return !in$(e.name, notUsed) && !in$(e.type, unwantedTypes);
};
run = function(){
// A blur handler to change values as they are typed. A submit handler to
// make sure that the browser didn't sneak a value in and not trigger the blur.
getInputs().forEach(function(e){
return e.addEventListener('blur', blurHandler);
});
return document.querySelector('.salsa form').addEventListener('submit', function(){
return getInputs().forEach(function(e){
return e.blur();
});
});
};
if (/action_KEY=\d+/.test(window.location.href)) {
if (document.getElementById('tempPostalCode') !== null) {
console.log("Waiting for the action to get to the letter page.");
actor = function(){
if (document.getElementsByName('First_Name').length !== 0) {
clearInterval(interval);
return run();
}
};
interval = setInterval(actor, 200);
}
} else {
run();
}
function in$(x, xs){
var i = -1, l = xs.length >>> 0;
while (++i < l) if (x === xs[i]) return true;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment