Calling a function from 2 different forms on different pages

B

Bryan

I have this function below

function checkLength(field, nextField)
{
var letters = document.form1.elements[field].value.length +1;
if (letters <= 2)
{document.form1.elements[field].focus()}
else
{document.form1.elements[nextField].focus()}
}

And i call it from here:
<form name="form1" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

My problem is that I have another form on a different page:
<form name="form2" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

When the call to check length from form2, it fails as document.form1
isn't an object. How do I need to alter the function keeping the 2
form names different to get the function to work ok?
Help greatly appreciated, sorry newbie!!!!
 
R

Randy Webb

Bryan said:
I have this function below

function checkLength(field, nextField)
{
var letters = document.form1.elements[field].value.length +1;
if (letters <= 2)
{document.form1.elements[field].focus()}
else
{document.form1.elements[nextField].focus()}
}

And i call it from here:
<form name="form1" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

My problem is that I have another form on a different page:
<form name="form2" method="POST" action="addComputer.php"
ONSUBMIT="return ValidateForm(this)">
Hardware Ethernet
<input type="text" name="hardwareEtherneta" maxlength=2 size=1
ONKEYUP="checkLength('1','2')">
</form>

When the call to check length from form2, it fails as document.form1
isn't an object. How do I need to alter the function keeping the 2
form names different to get the function to work ok?
Help greatly appreciated, sorry newbie!!!!

simplest way is to simply tell it the formname:

onkeyup="checkLength('form1','thisFieldName','nextFieldToFocus')"

function checkLength(formName,thisField, nextField)
{
letters = document.forms[formName].elemenets[thisField].value.length +1;
if (letters <= 2)
{document.forms[formName].elements[thisField].focus()}
else
{document.forms[formName].elements[nextField].focus()}
}

Could still be made a lot simpler, by passing references to the form and
the current field.

onkeyup="checkLength(this.form,this,'nextFieldName')"

I will leave it to you to try to code the second one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top