3MRAN 3MRAN - 3 months ago 24
jQuery Question

Change Text box value to Uppercase the first letter of every word separated by (.)

How can I change the value of textbox using jQuery/JavaScript before the form get submitted (before button clicked) when a user typed:

will.smith to Will.Smith

I have used the below code which only convert it Uppercase (entire string):

$('input[type=text]').val (function () {
return this.value.toUpperCase();
})


Keeping in mind I don't want to change it visually using CSS.

I hope someone can give me an idea on the same.

Answer
textBox.value = textBox.value.split('.').map(function(word) {
  return word[0].toUpperCase() + word.substr(1);
}).join('.');

Or since you are using jQuery:

$('input[type=text]').val(function() {
  return this.value.split('.').map(function(word) {
    return word[0].toUpperCase() + word.substr(1);
  }).join('.');
});

I'm not sure what you want to do with the value? But I think you get the idea.

Also, to be on the safe side you could do something like word.substr(1).toLowerCase() which would make sure that only the first letter is capitalized.