You are viewing limited content. For full access, please sign in.

Question

Question

In Forms fields, how can I ensure that the first letter of a name is always a capital?

asked on July 14, 2017

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).

2 0

Answer

SELECTED ANSWER
replied on July 14, 2017 Show version history

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:

  1. This works both ways, so if a user types all capital letters it will fix that as well
  2. 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();
  });
});

 

7 0

Replies

replied on July 14, 2017

You could do this one of two ways.

  1. 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.
  2. 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.

3 0
replied on July 14, 2017

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!

0 0
SELECTED ANSWER
replied on July 14, 2017 Show version history

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:

  1. This works both ways, so if a user types all capital letters it will fix that as well
  2. 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();
  });
});

 

7 0
replied on July 19, 2017

Wow, that worked slick!  Thanks!  Problem solved!

0 0
replied on October 15, 2019

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

0 0
replied on October 15, 2019

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

 

1 0
replied on November 17, 2020

Amazing!  Thank you for posting!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.