-2

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.

Omran Moh'd
  • 87
  • 1
  • 11

1 Answers1

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

adam-beck
  • 5,659
  • 5
  • 20
  • 34
  • what exactly I need to do with the value, I want to make sure before the form get submitted, whatever values typed by the user it should convert the same to exp: Will.Smith in the textbox – Omran Moh'd Aug 24 '16 at 21:11
  • I think you can do something like `this.value = this.value.split('.......`. That will update the textbox's text content. – adam-beck Aug 24 '16 at 21:12
  • thanks for the elaborated answer. Just one more thing, I can make the above code get executed when a user is typing in the textbox or it's not doable u think? – Omran Moh'd Aug 24 '16 at 21:21
  • You can. You would add an event listener that when the textbox changes. See https://api.jquery.com/change/. – adam-beck Aug 24 '16 at 21:30