Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to capitalize all words in all text input fields before the form is submitted. The supporter doesn't get to approve the changes -- they just happen.
<script>
// Submit handler to capitalize all of the text input fields in a form.Each
// word in each field is translated so that the first character
// is upper case and the remainder are lower case.
$(document).ready(function () {
var DO_NOT_MODIFY = "Email".split(',');
// Capitalize the first letter of a string.
// @return [String] returns the capitalized string
String.prototype.capitalizeFirstLetter = function() {
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
}
// Function to capitalize words in a string. This function capitalizes each
// word in a string, where a word is delimited by spaces.
// @return [String] returns the capitalized string
//
String.prototype.capitalizeWords = function() {
return this.split(' ')
.map(function(word) {
return word.capitalizeFirstLetter()
})
.join(' ');
}
// Function to uppercase the contents of each of the text inputs in
// a document *except* Email.
// @return [Boolean] always returns true
function capitalizeTextInputs() {
var list = document.getElementsByTagName('input');
for (var i = 0; i < list.length; i++) {
if (DO_NOT_MODIFY.indexOf(list[i].name) == -1 && list[i].type == 'text') {
list[i].value = list[i].value.capitalizeWords();
}
}
return true;
}
$('.salsa form').click(capitalizeTextInputs);
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment