make first character of each word capital in input
You can try this: DEMO
Name:<input type='text' name='name' class='name' style="text-transform: capitalize;" placeholder='Enter your name here'/>
or add text-transform: capitalize;
in your name
in css.
How to capitalize the first letter of a user-inputted string but keep the rest of the string's capitalization
Just edit the first character of each split to be upper:
# For this example, lets use this string. However, you would still use
# user_input = input("...") in your actual code
user_input = "for bar. egg spam."
# Turn the user_input into sentences.
# Note, this is assuming the user only is using one space.
# This gives us ["foo bar", "egg spam"]
sentences = user_input.split(". ")
# This is called a list comprehension. It's a way of writing
# a for-loop in Python. There's tons of documentation on it
# if you Google it.
#
# In this loop, the loop variable is "sentence". Please be mindful
# that it is a singular form of the word sentences.
#
# sentence[0].upper() will make the first letter in sentence uppercase
# sentence[1:] is the remaining letters, unmodified
#
# For the first iteration, this becomes:
# "f".upper() + "oo bar"
# "F" + "oo bar"
# "Foo bar"
capitalized_sentences = [sentence[0].upper() + sentence[1:]
for sentence
in sentences]
# At this point we have ["Foo bar", "Egg spam"]
# We need to join them together. Just use the same ". " we
# used to split them in the beginning!
#
# This gives us "Foo bar. Egg spam."
recombined_sentences = ". ".join(capitalized_sentences)
Replace "sentences" with your user_input
bit
Note, there might be a "gotcha" if the user inputs sentences of a format you aren't expecting. For example, what if the user entered two spaces instead of one? Then the above code would try to capitalize a whitespace character. You would need to account for that.
How can I capitalize the first letter of each word in a string?
The .title()
method of a string (either ASCII or Unicode is fine) does this:
>>> "hello world".title()
'Hello World'
>>> u"hello world".title()
u'Hello World'
However, look out for strings with embedded apostrophes, as noted in the docs.
The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
How can I capitalize the first letter of each word in a string using JavaScript?
You are not assigning your changes to the array again, so all your efforts are in vain. Try this:
function titleCase(str) { var splitStr = str.toLowerCase().split(' '); for (var i = 0; i < splitStr.length; i++) { // You do not need to check if i is larger than splitStr length, as your for does that for you // Assign it back to the array splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); } // Directly return the joined string return splitStr.join(' '); }
document.write(titleCase("I'm a little tea pot"));
Auto Capitalize ONLY the First Letter of Each Word in an Input Field
See comments inline for details:
// Get a reference to the input and wire it up to an input event handler that// calls the fixer functiondocument.getElementById("txtTest").addEventListener("input", forceLower);
// Event handling functions are automatically passed a reference to the// event that triggered them as the first argument (evt)function forceLower(evt) { // Get an array of all the words (in all lower case) var words = evt.target.value.toLowerCase().split(/\s+/g); // Loop through the array and replace the first letter with a cap var newWords = words.map(function(element){ // As long as we're not dealing with an empty array element, return the first letter // of the word, converted to upper case and add the rest of the letters from this word. // Return the final word to a new array return element !== "" ? element[0].toUpperCase() + element.substr(1, element.length) : ""; }); // Replace the original value with the updated array of capitalized words. evt.target.value = newWords.join(" "); }
<input type="text" id="txtTest">
Related Topics
Submit Input Doesn't Get The: Active State in Ie8 When I Click on The Button's Text
Create a Loop Using SCSS to Change Background Images with CSS3 Animate
CSS Column-Count Not Respected
Is There a Cross-Browser Way to Condense Text on a Page
Retrieve or Set Less Variable from JavaScript
Make an Image Fit Its Container by Setting The Image Height to The Containers Heights
Perfect Infinite Rotation for ֍ Character
Are There a CSS Property That Would Change Nothing and Where We Can Store Information
Ie9-11 Detecting Transform-Style: Preserve-3D
How to Expand a Cropped Image into a Box
Selenium How to Access Two Controls of Same CSS Class
CSS Buttons Rounded on One Side
CSS Target Just Class Name Starts with and Ends with String
Style Dojox Grid Row Depending on Data