Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass argument in a function

Status
Not open for further replies.

unixxxx

MIS
Oct 20, 2000
17
US
I did the script so that I can pass the arguments in a function. Everything seems fine except when I entered all fields, it will check each field instead of only checking one field at a time. As I have 3 "OnClick" button, how do I make each check button to only check its own field instead of checking all other fields?

Any clue?

<script type=&quot;text/javascript&quot;>
<!--

function checkValue(a,b,c)
{
if ((x=&quot;button1&quot;)&&(a!=''))
{
if(a>=5 && a<=45)
{
alert(&quot;'&quot; + a + &quot;' is Right!&quot;);
}
else
{
alert(&quot;'&quot; + a + &quot;' is wrong&quot;);
}
}

if ((x=&quot;button2&quot;)&&(b!=''))
{
if(b>=-123 && b<=467)
{
prompt(&quot;'&quot; + b + &quot;' is Right!&quot;);
}
else
{
prompt(&quot;'&quot; + b + &quot;' is wrong&quot;);
}
}

if ((x=&quot;button3&quot;)&&(c!=''))
{
if(c>=1 && c<=3)
{
window.status=&quot;'&quot; + c + &quot;' is Right!&quot;;
}
else
{
window.status=&quot;'&quot; + c + &quot;' is wrong&quot;;
}
}
}

-->
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<FORM NAME=&quot;myForm&quot;>

Enter a number between 5 and 45: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field1&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button1&quot; VALUE=&quot;Check&Alert&quot; onClick=&quot;checkValue(document.myForm.field1.value,document.myForm.field2.value, document.myForm.field3.value)&quot;><br>

Enter a number between -123 and 467: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field2&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button2&quot; VALUE=&quot;Check&Prompt&quot; onClick=&quot;checkValue(document.myForm.field1.value,document.myForm.field2.value,document.myForm.field3.value)&quot;><br>

Enter a number between 1 and 3: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field3&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button3&quot; VALUE=&quot;Check&Status&quot; onClick=&quot;checkValue(document.myForm.field1.value,document.myForm.field2.value,document.myForm.field3.value)&quot;><br>
</FORM>

</body>
</html>



 
unixxxx,

looks like you've forgotten to pass in a value for &quot;x&quot;.

personally I would simplify the function to accept two arguments: the name of the calling button, and the corresponding text field value:
[tt]
function checkValue(sName,sValue)
{
if ((sName=&quot;button1&quot;)&&(sValue!=''))
{
if(sValue>=5 && sValue<=45)
{
alert(&quot;'&quot; + sValue + &quot;' is Right!&quot;);
}
else
{
alert(&quot;'&quot; + sValue + &quot;' is wrong&quot;);
}
}

if ((sName=&quot;button2&quot;)&&(sValue!=''))
{
if(sValue>=-123 && sValue<=467)
{
prompt(&quot;'&quot; + sValue + &quot;' is Right!&quot;);
}
else
{
prompt(&quot;'&quot; + sValue + &quot;' is wrong&quot;);
}
}

if ((sName=&quot;button3&quot;)&&(sValue!=''))
{
if(sValue>=1 && sValue<=3)
{
window.status=&quot;'&quot; + sValue + &quot;' is Right!&quot;;
}
else
{
window.status=&quot;'&quot; + sValue + &quot;' is wrong&quot;;
}
}
}
[/tt]

call it like so:
[tt]
<input type=&quot;button&quot; name=&quot;button1&quot; value=&quot;Check&Alert&quot; onClick=&quot;checkValue(this.name,this.form.field1.value);&quot;
[/tt] =========================================================
if (!succeed) try();
-jeff
 
Actually, I would like to do function with three arguments, which is the field name or form object, the lower limit, and the upper limit. And return true if within range.

Am I doing it right in the beginning?

Please help. THanks
 
yes, it doesn't look like that's what a, b and c are being used for.

I think you want something like:
[tt]
function checkValue(oEl,iLower,iUpper) {
if(oEl.value>=iLower && oEl.value<=iUpper)
{
alert(&quot;'&quot; + oEl.value + &quot;' is Right!&quot;);
return true;
}
else
{
alert(&quot;'&quot; + oEl.value + &quot;' is wrong&quot;);
return false;
}
}
[/tt] =========================================================
if (!succeed) try();
-jeff
 
thanks for the reply. so if I do it that way, would it be possible to check each button with the value of different range?

 
yes, your form would look like so:
[tt]
<FORM NAME=&quot;myForm&quot;>

Enter a number between 5 and 45: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field1&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button1&quot; VALUE=&quot;Check&Alert&quot; onClick=&quot;checkValue(document.myForm.field1,5,45)&quot;><br>

Enter a number between -123 and 467: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field2&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button2&quot; VALUE=&quot;Check&Prompt&quot; onClick=&quot;checkValue(document.myForm.field2,-123,467)&quot;><br>

Enter a number between 1 and 3: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field3&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button3&quot; VALUE=&quot;Check&Status&quot; onClick=&quot;checkValue(document.myForm.field3,1,3)&quot;><br>
</FORM>
[/tt]


if you want to retain the three different methods of returning the answer as in your original script, you could modify it like so:
[tt]
function checkValue(sAnswer,sButton,iLower,iUpper)
{
if ((sButton=&quot;button1&quot;)&&(sAnswer!=''))
{
if(sAnswer>=iLower && sAnswer<=iUpper)
{
alert(&quot;'&quot; + sAnswer + &quot;' is Right!&quot;);
}
else
{
alert(&quot;'&quot; + sAnswer + &quot;' is wrong&quot;);
}
}

if ((sButton=&quot;button2&quot;)&&(sAnswer!=''))
{
if(sAnswer>=iLower && sAnswer<=iUpper)
{
prompt(&quot;'&quot; + sAnswer + &quot;' is Right!&quot;);
}
else
{
prompt(&quot;'&quot; + sAnswer + &quot;' is wrong&quot;);
}
}

if ((sButton=&quot;button3&quot;)&&(sAnswer!=''))
{
if(sAnswer>=iLower && sAnswer<=iUpper)
{
window.status=&quot;'&quot; + sAnswer + &quot;' is Right!&quot;;
}
else
{
window.status=&quot;'&quot; + sAnswer + &quot;' is wrong&quot;;
}
}
}

<FORM NAME=&quot;myForm&quot;>

Enter a number between 5 and 45: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field1&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button1&quot; VALUE=&quot;Check&Alert&quot; onClick=&quot;checkValue(document.myForm.field1.value,this.name,5,45)&quot;><br>

Enter a number between -123 and 467: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field2&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button2&quot; VALUE=&quot;Check&Prompt&quot; onClick=&quot;checkValue(document.myForm.field2.value,this.name,-123,467)&quot;><br>

Enter a number between 1 and 3: <INPUT TYPE=&quot;TEXT&quot; SIZE=&quot;10&quot; NAME=&quot;field3&quot;>
<INPUT TYPE=&quot;BUTTON&quot; NAME=&quot;button3&quot; VALUE=&quot;Check&Status&quot; onClick=&quot;checkValue(document.myForm.field3.value,this.name,1,3)&quot;><br>
</FORM>

[/tt]
=========================================================
if (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top