Skip to content Skip to sidebar Skip to footer

How To Capitalise First Letter Of String In Javascript?

I have created one form. In that form there are 10 textbox and one button. I would like that when the user enters text into textbox and loses focus, the first letter of the string

Solution 1:

Do it like this...

page_load()
{
    TextBox1.Attributes.Add("onblur", "capitaliseFirstLetter('" + TextBox1.ClientID + "')");
}

Modify your JavaScript function as below...

functioncapitaliseFirstLetter(txtID) {
    var txtString = document.getElementById(txtID).value;
    document.getElementById(txtID).value = txtString.charAt(0).toUpperCase() + txtString.slice(1);
} 

Solution 2:

You can declare this onblur event on aspx page like below.

<asp:TextBoxID="TextBox1"runat="server"onblur="capitaliseFirstLetter(this)"></asp:TextBox>

Then the javaScript will be like below.

<scriptlanguage="javascript">functioncapitaliseFirstLetter(textBox)
     {
         return textBox.value.charAt(0).toUpperCase() + string.slice(1);
     } 
</script>

Solution 3:

console.log(str.charAt(0).toUpperCase() + str.slice(1));

Solution 4:

very simple answer with variables with two lines

capitalised string is only for first letter, rest will remain lower case

var sentence = "string of worlds"var capitizedString = (sentence.slice(0,1)).toUpperCase() + (sentence.slice(1)).toLowerCase();

Post a Comment for "How To Capitalise First Letter Of String In Javascript?"