...

View Full Version : Upper and Lower Case in Text field



Kylena
09-29-2003, 03:21 AM
Hi, I've the following code in my field


var pattern = /(\w)(\w*)/; // a letter, and then one, none or more letters
var a = document.forms[0].LName.value.split(/\s+/g); // split the sentence into an array of words

for (i = 0 ; i < a.length ; i ++ ) {
var parts = a[i].match(pattern); // just a temp variable to store the fragments in.

var firstLetter = parts[1].toUpperCase();
var restOfWord = parts[2].toLowerCase();

a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
}

document.forms[0].LName.value = a.join(' ');
var f = document.forms[0];
f.FullName.value = f.LName.value + " " + f.FName.value;

whereby document.forms[0] is equivalent to this.form.

It works, however, I'd like to modify it so that when users enter data in the field, not all capitals appear and not all lowercase letters.

Eg:

AT THE HOUSE would appear as

At the house

The above code would change it to

At The House (which is not what I want)

Willy Duitt
09-29-2003, 06:14 AM
This will Capitalize the first letter of a string. If you want to capitalize more than one input (sentence) you will have to split the array at periods(.) and loop thru for any other puncuation before you fire this script.

<HTML>
<HEAD>
<SCRIPT type="text/javascript">
<!--//
function capitalizeFirst(form){
s=form.t1.value;
s=s.toLowerCase();

/* Using a substring, we capitalize the first letter.
If the first character is not alphabetical, then this will not work.
Eg: using a number */
s=s.substring(0,1).toUpperCase()+s.substring(1,s.length);
form.t1.value=s;
}
//-->
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<FORM NAME="" METHOD="POST" ACTION="" ENCTYPE="TEXT/PLAIN">
<INPUT TYPE="TEXT" NAME="t1" VALUE="AT THE HOUSE" SIZE="32">
<br>
<INPUT TYPE="BUTTON" VALUE="Capitalize first letter only" onClick="(capitalizeFirst(form));return false">
</FORM>
</BODY>
</HTML>


Hope this helps :)
.....Willy

Kylena
09-29-2003, 11:15 AM
Thanks Willy. It works just like I wanted. I'd like to know how do I loop through the whole field like when the user has to type the following:

I (will change to i, if this code is used.)
periods (. the next letter should change to capital)

I can't see how the loop should run.
:confused:

Kylena
10-29-2003, 03:41 AM
I'd like to modify the first code to include commas (,).

How can I do that?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum