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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onKeyPress question 4

Status
Not open for further replies.

schase

Technical User
Sep 7, 2001
1,756
US
Howdy again,

I use
onKeyPress="this.style.textTransform='capitalize';

on some form fields, that will after submitting, insert via asp into an access database.

Curiously, even though the onKeyPress works while typing the form - upon entry it reverts back to as it was typed

e.g. typed stuart which shows as Stuart, inserts into the database as stuart

any ideas how to retain the case?

Thank you "Never underestimate the power of determination"

Stuart
 
i found a workaround using javascript and the onkeyup event thinger :
Code:
<html>
<head>
<script language=&quot;JavaScript&quot;>
function capIt(v)
{
if (v.value.length == 1){
newVal = v.value.toUpperCase();
v.value = newVal;
}
}
</script>
</head>
<body>
<input type='text' onKeyUp=&quot;capIt(this)&quot;>
</body>
</html>
hope it helps! &quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
That only changes the first letter of the first word. To change every word, you will want to do this:

<script language=&quot;JavaScript&quot;>
<!--
function capitalize(){
var words=document.the_form.to_caps.value.split(&quot; &quot;);
var caps=&quot;&quot;;
for(var i=0; i<words.length; i++){
temp_letter=words.substring(0,1);
temp_letter=temp_letter.toUpperCase();
words=temp_letter+words.substring(1,words.length);
caps=caps+&quot; &quot;+words;
}
document.the_form.to_caps.value=caps;
}
//-->
</script>

<form name=&quot;the_form&quot; onSubmit=&quot;capitalize();&quot;>
<input type='text' name=&quot;to_caps&quot; onKeyPress=&quot;this.style.textTransform='capitalize';&quot;>
</form>

The onKeyPress makes the text appear capitalized to the user, and the capitalize() function makes the text actually capitalized.

Rick
 
try this:
Code:
<html>
<head>
<script language=&quot;JavaScript&quot;>
function capIt(v)
{
dStuff = (v.value.indexOf(&quot; &quot;)!=-1) ? v.value.split(&quot; &quot;) : v.value;
if(dStuff==v.value){
v.value =  v.value.charAt(0).toUpperCase()+v.value.substring(1,v.value.length+1);
}
else {
for (d=0;d<v.value.length;d++)
	{
	if (v.value.charAt(d) == ' ' && v.value.charAt(d+1))
		{
		v.value = v.value.substring(0,d) + &quot; &quot; +v.value.charAt(d+1).toUpperCase() +  v.value.substring(d+2,v.value.length+1);
		}
	}

}
}
</script>
</head>
<body>
<input type='text'
	onKeyUp=&quot;capIt(this)&quot;>
</body>
</html>

hope that helps &quot;Those who dare to fail miserably can achieve greatly.&quot; - Robert F. Kennedy
So true..
 
Ah! I forgot to take off TGML! You beat me;-). My above code:

<script language=&quot;JavaScript&quot;>
<!--
function capitalize(){
var words=document.the_form.to_caps.value.split(&quot; &quot;);
var caps=&quot;&quot;;
for(var i=0; i<words.length; i++){
temp_letter=words.substring(0,1);
temp_letter=temp_letter.toUpperCase();
words=temp_letter+words.substring(1,words.length);
caps=caps+&quot; &quot;+words;
}
document.the_form.to_caps.value=caps;
}
//-->
</script>

<form name=&quot;the_form&quot; onSubmit=&quot;capitalize();&quot;>
<input type='text' name=&quot;to_caps&quot; onKeyPress=&quot;this.style.textTransform='capitalize';&quot;>
</form>

Rick
 
It works,

Thank you so much, and please accept my apologies for the very tardy reply.

&quot;Never underestimate the power of determination&quot;

Stuart
 
Guys,
Stuart put me onto the code for Capitalising the First letter of every word and it works great except if the &quot;Caps Lock&quot; is on or the user presses the shift key whilst typing. Is there anyway i could put code in to disable &quot;Caps Lock&quot; Regards

Paul
 
Sure can. This will disable Caps Lock:
Code:
<script>
<!--
document.onkeydown = getKey;

function getKey() {
	if (event.keyCode == 20) return false;
}
//-->
</script>
 
Supra,
Sorry to be a pain but where would i place this script in my code
Regards

Paul
 
You would place this in your <head>. Note--this will prevent the user from pressing the CAPS LOCK key WHILE THE WEBPAGE IS THE ACTIVE WINDOW. If it was pressed any other time, it would work fine--meaning that if CAPS LOCK was on when the user entered the page, it would not fix you rproblem.

Rick -----------------------------------------------------------
 
Quite an intresting discussion. So basically what you want the ability to turn user's capslock on or off. Well, sorry I dont have an answer as I am new to the javascript world. But, I'll be quite surprised and amazed to know that if this is possible at all. I dont think javascript can do that.
 
I dont think Paul was looking for total disable of caps lock per say.

Rather force lowercase (or change to lowercase) and allow it to be entered into a database in the same lowercase. &quot;Never underestimate the power of determination&quot;

Stuart
 
here's a simple way to ensure all input is lowercase:

<textarea onchange=&quot;this.value = this.value.toLowerCase();&quot;></textarea>

=========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top