Jump to content


Photo

Cant get the textfield name/id


  • Please log in to reply
5 replies to this topic

#1 Thierry

Thierry
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationNetherlands

Posted 26 May 2006 - 12:07 PM

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...

<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>

This would be a textfield that uses it.

<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"

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?
Have you tried....a reset?

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 26 May 2006 - 05:40 PM

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
function check_date(ob){
     var val = ob.value;
}

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

NoGray.com


#3 Thierry

Thierry
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationNetherlands

Posted 31 May 2006 - 07:30 AM

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.
Have you tried....a reset?

#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 31 May 2006 - 05:49 PM

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.

NoGray.com


#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 01 June 2006 - 11:12 AM

You are passing in a string value as the parameter and trying to use it as the object reference. You need to instead use

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

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

Or you could try

fieldObj = document.getElementById(fieldname)

fieldObj.value = '2006'

HTH.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#6 Thierry

Thierry
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationNetherlands

Posted 01 June 2006 - 12:52 PM

Seems to work, thanks :)
Have you tried....a reset?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users