Thierry Posted May 26, 2006 Share Posted May 26, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/10500-cant-get-the-textfield-nameid/ Share on other sites More sharing options...
nogray Posted May 26, 2006 Share Posted May 26, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10500-cant-get-the-textfield-nameid/#findComment-39236 Share on other sites More sharing options...
Thierry Posted May 31, 2006 Author Share Posted May 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10500-cant-get-the-textfield-nameid/#findComment-40473 Share on other sites More sharing options...
nogray Posted May 31, 2006 Share Posted May 31, 2006 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.valueIf you have the page up somewhere I can take a quick look at it. Quote Link to comment https://forums.phpfreaks.com/topic/10500-cant-get-the-textfield-nameid/#findComment-40636 Share on other sites More sharing options...
king arthur Posted June 1, 2006 Share Posted June 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10500-cant-get-the-textfield-nameid/#findComment-40862 Share on other sites More sharing options...
Thierry Posted June 1, 2006 Author Share Posted June 1, 2006 Seems to work, thanks :) Quote Link to comment https://forums.phpfreaks.com/topic/10500-cant-get-the-textfield-nameid/#findComment-40893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.