Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

uppercase first char in FirstName and LastName text fields 1

Status
Not open for further replies.

onressy

Programmer
Mar 7, 2006
421
CA
Howdy all, i have a form that has two text fields, FirstName & LastName

How can i use the onChange event to handle the changing of the users value in FirstName to have a capital character at the beining of their input, so if they enter "scott" onChange would result in "Scott"

Thanks!
 
Something like this will work. Note - I've chose to use onBlur rather than onChange, as you don't really want to go messing with text field values with the caret still in the field (I hate this, and so imagine other users might find it annoying too).

Code:
<input type="text" ... onblur="this.value = this.value.substr(0, 1).toUpperCase() + this.value.substr(1);">

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Of course, if you want the same functionality for multiple fields, you'd be better off creating a function in your script section:

Code:
function initialCap(field) {
   field.value = field.value.substr(0, 1).toUpperCase() + field.value.substr(1);
}

and then using:

Code:
<input type="text" ... onblur="initialCap(this);">

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thank Dan,
Question: what does this mean: substr(0, 1)

also, would i need to get the length of the string in order to set the remaining characters of the FirstName string to lowercase, if they arn't.
hmm.... maybe it would be easier to set all characters in FirstName to lowerCase first then set the first character to upper case. What about a situation where the user enters an initial then the name, like DR. JOHN or dr. john
at which case i would require, Dr. John

Thanks for you knowledge!
 
substr(0, 1)" gets the first character from the string (starting at character 0 for 1 character in length).

also, would i need to get the length of the string in order to set the remaining characters of the FirstName string to lowercase, if they arn't.

No - you would not need to get the length to do this. Simply add ".toLowerCase()" after the ".substr()" call. I didn't do this because you didn't ask for this - you only asked for the case of the initial letter to be changed.

What about a situation where the user enters an initial then the name, like DR. JOHN or dr. john
at which case i would require, Dr. John

That's a new ball game altogether. Perhaps you would be better off speccing out your complete requirements and posting them, rather than adding scenarios on a case-by-case basis.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top