Jump to content

Capitalize 1st Character only


gabe8496

Recommended Posts

I use a text field to allow a user to specify custom text and on submit, it generates a image with embedded text.

Right now, i'm getting it to uppercase all 1st letter of each word...HOWEVER, I have some issues where a customer can have their caps lock and all their text is set to uppercase, which is a problem.

Here is teh script I'm using right now:

[code]
<script LANGUAGE="JAVASCRIPT">
<!--

function capitalizeMe(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        obj.value = newVal;
}

// -->
</SCRIPT>
[/code]

So, in the end...if the user types SAN BENITO, TEXAS, I want it to change to => San Benito, Texas

I'm not all that familiar with javascript, so if anyone can help me out and explain how it works, I would greatly appreciate it.

Thanks!

Gabe
Link to comment
https://forums.phpfreaks.com/topic/9318-capitalize-1st-character-only/
Share on other sites

[!--quoteo(post=372320:date=May 8 2006, 11:58 AM:name=lead2gold)--][div class=\'quotetop\']QUOTE(lead2gold @ May 8 2006, 11:58 AM) [snapback]372320[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = ucfirst(strtolower($str));
echo $str; // Prints: Mary had a little lamb and she loved it so
[/code]
[/quote]
Thanks for that info.

However, I don't think that would work in my situation.

How it works right now:

User types: san benito, texas
Field updates to: San Benito, Texas

This is perfect...but

If the user does the following:
User types: SAN BENITO, TEXAS
Form Field updates to: SAN BENITO, TEXAS

As you can see, the field stays in all caps. I would need the output (if the user types all caps) to go to => San Benito, Texas

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.