Skip to content

Instantly share code, notes, and snippets.

@salsalabs
Last active August 29, 2015 14:26
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/7844c4a1323b2561d905 to your computer and use it in GitHub Desktop.
Save salsalabs/7844c4a1323b2561d905 to your computer and use it in GitHub Desktop.
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