Jump to content

Archived

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

Thierry

Cant get the textfield name/id

Recommended Posts

I made a small script which prevents the user from typing anything in a textfield that isnt a number.
That works, but right now I have to copy that script for each text field in order for it to work.
So I added another thing to my function, in which you enter the fieldname so I dont have to copy the script each time. But for some reason, it doenst seem to take the fieldname with it...

[code]<script type="text/javascript">
<!--
function check_date(datevalue,fieldname){
var number = 0;
while(number < datevalue.length){
var datepiece = datevalue.substring(number, number+1);
if(datepiece < "0" || datepiece > "9"){document.form1.fieldname.value = "2006";}
++number;}

}
-->
</script>[/code]

This would be a textfield that uses it.

[code]
<input name="numbercode" onKeyDown="check_date(numbercode.value,'numbercode')" onKeyUp="check_date(numbercode.value,'numbercode')" onBlur="check_date(numbercode.value,'numbercode')" type="text" id="numbercode"[/code]

It gets the value from the textfield just fine, but it doenst seem to take the 'numbercode' name with it, and thus it doesnt find the textfield. (saying that document.form1.fieldname doesnt exist).

Any ideas to what Im doing wrong?

Share this post


Link to post
Share on other sites
It's best if you use "this" for the field (like onKeyUp="check_date(this);") which will send the complete object to the function. You can access the value of the object in the function like this
[code]
function check_date(ob){
     var val = ob.value;
}
[/code]

Also you can try to change "fieldname" to something that is not generic (like fldnm) and see if it works.

Share this post


Link to post
Share on other sites
Nope, still doesnt work, it still says it cant find the field, its looking to replace document.form1.fldnm.value but ofcourse that fieldname doesnt exist.

Share this post


Link to post
Share on other sites
You'll need to make sure all fields have unique names and ids and if you pass the object to the function instead of the value, you don't need to use "document.form1.fldnm.value", just use object.value

If you have the page up somewhere I can take a quick look at it.

Share this post


Link to post
Share on other sites
You are passing in a string value as the parameter and trying to use it as the object reference. You need to instead use

[code]document.forms["yourformname"].elements[fieldname][/code]

if you only have one form on the page you then "yourformname" can be 0.

Or you could try

[code]fieldObj = document.getElementById(fieldname)

fieldObj.value = '2006'[/code]

HTH.

Share this post


Link to post
Share on other sites

×

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.