...

View Full Version : Calling a function with an argument



jpeg
12-21-2005, 05:53 PM
I wrote a script that checks if a form field has numbers using the isNaN() function. This part works, but since I have to do this for many fields, I't thouht I'd send the false results to a function. This part dosn't work. Maybe I am not writting the code properly. If anyone can help me, it would be appreciated. The code is below:


if (!isNaN(HardwareSoftwareForm.PC_Quantity.value)) {
errorNumMessage("Extension");
}

function errorNumMessage(thisVar) {
alert ("You Have Entered an Invalid Value for "+thisVar+". Please Enter Only Numbers");
HardwareSoftwareForm.+thisVar+.focus()
return false;
}

Nischumacher
12-21-2005, 06:14 PM
try this

if (!isNaN(HardwareSoftwareForm.PC_Quantity.value)) {
return errorNumMessage("Extension");
}
or you could use regular expressions...

function checkNumeric(objElement)
{ var re = /^[0-9]*$/;
if (!re.test(objElement.value))
{ alert("You Have Entered an Invalid Value for "+objElement+". Please Enter Only Numbers!");
objElement.value = objElement.value.replace(/[^0-9]/g,"");
objElement.focus();
objElement.select();
return false;
}
}

Pyth007
12-21-2005, 09:47 PM
The problem with your function is in this line:

HardwareSoftwareForm.+thisVar+.focus()

Assuming that thisVar is the name of your element, you can try changing it to:

document. HardwareSoftwareForm.thisVar.focus();

(I added "document" to the beginning, got rid of the +'s around thisVar, and put a semi-colon at the end). Note also that this requires that your fields have unique names in order for this to work properly (normally only id's need to be unique).

Nischumacher's function would also work (I'd actually prefer this function...). In order to run this function correctly, you'd probably call it using the field's onchange event:

<input type="text" name="Quantity" id="numericField1" onchange="checkNumeric(this)">
...although you could also do it during the form's onsubmit; you'd need to cycle through each element in the form in order to do this. Note that Nischumacher's function passes an object (which is why I used "this" when calling it). A slight change to the function to make it work correctly:

alert("You Have Entered an Invalid Value for "+objElement.name+". Please Enter Only Numbers!");
This assumes that the field's name had meaning to the user...

One last thing... it's been noted that IE may have problems with the .focus() and .select() calls (esp. when the functions are being called from either onblur or onchange events). Two things you may need to do: 1) set "returnValue=false;" in addition to "return false;" and 2) use setTimeout("objElement.focus()", 100); to allow the event function to complete before trying to shift focus.

Nischumacher
12-22-2005, 03:03 PM
A slight change to the function to make it work correctly:

alert("You Have Entered an Invalid Value for "+objElement.name+". Please Enter Only Numbers!");
This assumes that the field's name had meaning to the user...
you are right... thank you for correcting me... and for explaning as well...


One last thing... it's been noted that IE may have problems with the .focus() and .select() calls (esp. when the functions are being called from either onblur or onchange events). Two things you may need to do: 1) set "returnValue=false;" in addition to "return false;" and 2) use setTimeout("objElement.focus()", 100); to allow the event function to complete before trying to shift focus.
interesting info... thanks again...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum