Description:

In this article, I will explain how to convert first letter of string to uppercase using Jquery.

In some scenario, we may require to read person name. For the name field we need to convert first letter of every word in capital letter.

Create a function that uses keyup() method, which will identify the first character of each word and it will convert to uppercase.

We can covert each first character of the word by using css also.

text-transform: capitalize; - This property is used to convert first letter of each word using css.

Example program:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#txtName').on('keyup', function (event) {
                name = $(this).val();
                name = name.toLowerCase().replace(/\b[a-z]/g, function (firstletter) {
                    return firstletter.toUpperCase();
                });
                $(this).val(name);
            });
        });
    </script>
    <%-- We can convert first letter of a string Using CSS too.
        <style>
        #TextBox1
        {
            text-transform: capitalize;
        }
    </style>    --%>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblEnterName" runat="server" Text="Enter Name:"></asp:Label>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>


Convert first letter of each word to capital letter. Converting the first letter of a string to Uppercase, How to convert first letter of each word to uppercase, Convert First Letter to Uppercase using Jquery, Convert all first letter to Uppercase using Jquery.

0 comments:

Post a Comment

 
Top