Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/10500-cant-get-the-textfield-nameid/
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.
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.
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.