In Forms fields, how can I ensure that the first letter of a name is always a capital? I'm getting individuals not bothering to type them in right (ie, john doe) and then further business process steps are looking unprofessional (ie, a response email to the applicant).
Question
Question
In Forms fields, how can I ensure that the first letter of a name is always a capital?
Answer
There's probably a dozen different ways to accomplish this, but making the change on the "blur" event when the user exits the field is a good option and it gives immediate results so testing will be easier.
//triggers when the page loads $(document).ready(function(){ //assign blur event to update value when user exits the field $('#q1 input').blur(function(){ //get name and clean up leading/trailing whitespace var name = $(this).val().trim(); //replace first letter of each word with uppercase name = name.toLowerCase().replace(/\b[a-z]/g, function(letter) { return letter.toUpperCase(); }); //save the updated value back into the field $(this).val(name).change(); }); });
Just replace "#q1 input" with your field, and you can get the "q" number on the css/javascript page (I'm assuming it is a single line field, input, but a multi-line would be textarea instead).
NOTE: There's two things to keep in mind for this code:
- This works both ways, so if a user types all capital letters it will fix that as well
- This does not account for hyphenated names so you might need to add a little more code to make it more robust.
It is also possible to consolidate these tasks into a much more compact form (see below), but I left them separated so you could get a better idea of what was actually happening in each step and troubleshoot more easily.
//triggers when the page loads $(document).ready(function(){ //assign blur event to update value when user exits the field $('#q1 input').blur(function(){ //every step crammed into one package $(this).val($(this).val().trim().toLowerCase().replace(/\b[a-z]/g, function(letter) { return letter.toUpperCase(); })).change(); }); });
Replies
You could do this one of two ways.
- Apply regex to the field to force users to capitalize the first letter of each word and generate a validation error message if they do not.
- Let users be users and use JavaScript/JQuery to format the field for them either after the field blurs, or when they click the submit button.
Personally, I prefer option 2 because users can get frustrated with formatting rules like that, and it is usually easier to just fix the input with the form instead.
Thanks, Jason. For the JavaScript/JQuery option, what exactly would that look like? I only have a very brief understanding and a few examples of how to put those together.
Thanks!
There's probably a dozen different ways to accomplish this, but making the change on the "blur" event when the user exits the field is a good option and it gives immediate results so testing will be easier.
//triggers when the page loads $(document).ready(function(){ //assign blur event to update value when user exits the field $('#q1 input').blur(function(){ //get name and clean up leading/trailing whitespace var name = $(this).val().trim(); //replace first letter of each word with uppercase name = name.toLowerCase().replace(/\b[a-z]/g, function(letter) { return letter.toUpperCase(); }); //save the updated value back into the field $(this).val(name).change(); }); });
Just replace "#q1 input" with your field, and you can get the "q" number on the css/javascript page (I'm assuming it is a single line field, input, but a multi-line would be textarea instead).
NOTE: There's two things to keep in mind for this code:
- This works both ways, so if a user types all capital letters it will fix that as well
- This does not account for hyphenated names so you might need to add a little more code to make it more robust.
It is also possible to consolidate these tasks into a much more compact form (see below), but I left them separated so you could get a better idea of what was actually happening in each step and troubleshoot more easily.
//triggers when the page loads $(document).ready(function(){ //assign blur event to update value when user exits the field $('#q1 input').blur(function(){ //every step crammed into one package $(this).val($(this).val().trim().toLowerCase().replace(/\b[a-z]/g, function(letter) { return letter.toUpperCase(); })).change(); }); });
Wow, that worked slick! Thanks! Problem solved!
Hello Jason,
What if I wanted this to happen on a repeatable collection field? This code works perfectly for the first field in the repeatable collection, but not the ones that users add after.
Thanks,
Mui
Hi Mui,
I wrote another post awhile back that should help with applying this to a table or collection.
https://answers.laserfiche.com/questions/153832/Table-and-Collection-JavaScript-Event-Handlers
Amazing! Thank you for posting!