Converting form field data to uppercase

N

Nige

Is it possible to convert an item of form field data to uppercase using
the toUpperCase() method?

What I'm really asking is, how do I reference the data item?
 
V

VK

document.FormName.FieldName.value =
document.FormName.FieldName.value.toUpperCase();
or
with (document.FormName) {
FieldName.value = FieldName.value.toUpperCase();
/* all the same, just "cooler" looking and shorter*/
}
 
R

Raul Carrillo Garrido

Maybe this script helps you. Has been taken from The Javascript Source.-
http://javascriptsource.com


<!-- ONE STEP TO INSTALL ALL UPPER CASE:

1. Copy the coding into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the BODY of your HTML document -->

<BODY>

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<center>
<form name="capsform">
<input type="text" name="caps" size=40 value=""
onChange="javascript:this.value=this.value.toUpperCase();">
<br>
<input type="button" value="Ok!">
</form>
</center>

<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 0.55 KB -->

Metsuke <(^_^)>
 
E

Elmo Watson

This works well for me, but what I'm trying to do is create a function that
does this so I can use the same thing in all form fields and I just can't
get it....
I've tried various forms of what you see below and it never works:
<SCRIPT LANGUAGE="Javascript">
<!-- Beginning of JavaScript -
function convcaps(this)
{
document.form.this.value=document.form..this.value.toUpperCase();
}
// - End of JavaScript - -->
</SCRIPT>

Then, in the input box:
<input name="textbox1" type="text" id="textbox1"
onChange="convcaps(textbox1);">

I keep getting an 'Expected Identifier' error in the main line of the
function - -

What am I doing wrong here?

Thanks.
 
L

Lasse Reichstein Nielsen

Elmo Watson said:
<SCRIPT LANGUAGE="Javascript">
<!-- Beginning of JavaScript -
function convcaps(this)
{
document.form.this.value=document.form..this.value.toUpperCase();
}
// - End of JavaScript - -->
</SCRIPT>
What am I doing wrong here?

Top posting. Please trim your quotes instead of including the entire
message you respond to.

In HTML 4, the type attribute is required on the script tag. The
recommended way to start a script element is:
<script type="text/javascript">

You don't need the <!-- line.

And, the important errors:

1) "this" is a keyword in Javascript, you can't use it as a variable.

2) you have a variable, call it "variableName" since "this" is not
legal. It contains a string with the name of a property of the form.
You then write.
document.form.variableName.value
This finds the property with the name "variableName" in the form, not
the one with name that is in the string that variableName refers to.
To use a variable to look up a property, you must use square brackets:
document.form[variableName].value
<URL:http://jibbering.com/faq/#FAQ4_39>

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
1) "this" is a keyword in Javascript, you can't use it as a variable.

More, `this' is a special operator and (therefore) a reserved word.


PointedEars
 
R

Richard Cornford

Thomas 'PointedEars' Lahn said:
More, `this' is a special operator and (therefore) a reserved word.

That would explain why ECMA 262 Section 7.5.2 lists "this" specifically
under the heading "keywords".

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top