Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript


I want to change the first letter into Caps though the user Enters the Small letter in text box control.



please give me the code..
Posted

Could be useful for you,
C#
function capitaliseFirstLetter(string)
{
    return string.charAt(0).toUpperCase() + string.slice(1);
}
 
Share this answer
 
Comments
Rohini Palanichamy 21-Nov-12 1:22am    
where can i write it .If i am writing it in client side means ,I thing
string is not valid in java script.In server side,The charAt() is not valid.I have included the imports system.string header file.
Try this. its working well. :-)

HTML
<script type="text/javascript">

JavaScript
String.prototype.capsFirst=function() {
return this.substr(0,1).toUpperCase()+ this.substr(1);
}
function doCompareDate() {
var name = document.getElementById("mytext").value;
document.getElementById("mytext").value=name.trim().capsFirst();
}

HTML
</script>


example.
http://jsfiddle.net/4F7pH/1/
 
Share this answer
 
v2
Comments
Rohini Palanichamy 21-Nov-12 1:48am    
This is my code.I got it by changing only the ID in your code

String.prototype.capsFirst=function() {
return this.substr(0,1).toUpperCase()+ this.substr(1);
}
function UpperCase1()
{
alert("Hai");
var name = document.getElementById("EmpName").value;
document.getElementById("EmpName").value=name.trim().capsFirst();
}



I called it in the onblur event of the "EmpName" Textbox.The control is going to the
function.
I got the "Alert" Msg.But the Remining is not working.

I have all other java scripts in my code which are working properly
deepak.m.shrma 21-Nov-12 2:10am    
nope its working.. try this.

<!DOCTYPE html>
<html>
<body>

<p id="demo">Onblur text will change.</p>

Try it <input type="text" id="EmpName" önblur="UpperCase1()"/>

<script type="text/javascript">
String.prototype.capsFirst=function()
{
return this.substr(0,1).toUpperCase()+ this.substr(1);
}
function UpperCase1()
{ alert("Hai");
var name = document.getElementById("EmpName").value;
document.getElementById("EmpName").value=name.trim().capsFirst();
}
</script>

</body>
</html>


example:
http://jsfiddle.net/4F7pH/2/
Rohini Palanichamy 21-Nov-12 2:22am    
you are spending your time a more.Thanks For this ,I already used the HTML Text type only.But it is not working.My HTML code is


<input id="txtEmpName" runat="server" onfocus="clear1();" onkeypress="ValidateAlpha();" önblur="UpperCase1()"
style="z-index: 101; left: 176px; position: absolute; top: 64px" type="text" />
deepak.m.shrma 21-Nov-12 3:45am    
will you post your whole htlm..it'll very easy to debug your core. where you loosing focus.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900