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 Chriss Miller 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 three arguments in a function. PLEase help

Status
Not open for further replies.

unixxxx

MIS
Oct 20, 2000
17
US
I have problems with this code:

If i only called the first form, it will work correctly. But how do I call the second argument and third argument in the same function?

Please advice. THanks.


form name="Form">
Enter a number between 5 and 45: <input type=&quot;text&quot; name=&quot;Field&quot; size=&quot;5&quot;>
<input type=&quot;button&quot; value=&quot;Check & Alert&quot; onClick=&quot;CheckValue()&quot;> <br>
Enter a number between -123 and 467: <input type=&quot;text&quot; name=&quot;Field&quot; size=&quot;5&quot;>
<input type=&quot;button&quot; value=&quot;Check & Prompt&quot; onClick=&quot;CheckValue()&quot;> <br>



</form>

<SCRIPT>
function CheckValue(a,upper,lower)
{
a = eval(document.Form.Field.value)
upper=eval(document.Form.Field2.value)

if ( a >= 5 && a <= 45) {
alert(&quot;You are OK.&quot;)
}
else {
alert(&quot;You are not OK.&quot;)
}
if (upper<-123 && upper>467){
prompt(&quot;You are not OK.&quot;)
}

</SCRIPT>
 
Remove the following lines from the function:
a = eval(document.Form.Field.value)
upper=eval(document.Form.Field2.value)

And do this:
<input type=&quot;button&quot; ... onClick=&quot;CheckValue(document.Form1.Field1.value, document.Form.Field2.value)&quot;>

I don't see where is the need in 3rd parameter - you don't use it.
 
I actually need three arguments in a function. I need to create one function that will check any field to see if its value is within a range and return a true or false value The function should expect three arguments:
the name of the field (form object)
the lower limit
the upper limit

And I am doing it this way:
function alertCheck()
{
a = document.myForm.field1.value;
if(5<=a && a<=45)
{
alert(&quot;'&quot; + a + &quot;' is Right!&quot;);
}
else
{
alert(&quot;'&quot; + a + &quot;' is Wrong! pls enter again&quot;);
}
}

function promptCheck()
{
b = document.myForm.field2.value;

if(-123<=b && b<=467)
{
prompt(&quot;'&quot; + b + &quot;' is Right!&quot;);
// promptValue = prompt(&quot;'&quot; + b + &quot;' is Wrong! Pls try again&quot;);
// if (-123>=b && b>=467) continue
// break;
}
else
{
bValue = prompt(&quot;'&quot; + b + &quot;' is Wrong! Pls try again&quot;);
if (-123<=bValue && bValue<=467)
{
alert(&quot;Thank You&quot;);
}
else
{
alert(&quot;Nope, try again in the main page&quot;);
}
}
}

function statusCheck()
{
c = document.myForm.field3.value;
if(1<=c && c<=3)
{
window.status =&quot;'&quot; + c + &quot;' is Right!&quot;;
}
else
{
window.status =&quot;'&quot; + c + &quot;' is Wrong!&quot;;
}
}

Is there any way I can combine these 3 functions into one? means there are three arguments in a function, instead of each argument in a function?

Thanks a lot :(
 

post the entire code, html and javascript...i'll hook it up.

- spewn
 
Thanks for the fast reply. Here is the whole code :

How do I put these three functions into a function with three arguments?

Thanks again

<html>
<head>
<title>Untitled Document</title>

<script type=&quot;text/javascript&quot;>
<!--
function alertCheck()
{
a = document.myForm.field1.value;
if(5<=a && a<=45)
{
alert(&quot;'&quot; + a + &quot;' is Right!&quot;);
}
else
{
alert(&quot;'&quot; + a + &quot;' is Wrong! pls enter again&quot;);
}
}

function promptCheck()
{
b = document.myForm.field2.value;

if(-123<=b && b<=467)
{
prompt(&quot;'&quot; + b + &quot;' is Right!&quot;);
// promptValue = prompt(&quot;'&quot; + b + &quot;' is Wrong! Pls try again&quot;);
// if (-123>=b && b>=467) continue
// break;
}
else
{
bValue = prompt(&quot;'&quot; + b + &quot;' is Wrong! Pls try again&quot;);
}
}

function statusCheck()
{
c = document.myForm.field3.value;
if(1<=c && 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;alertCheck()&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;promptCheck()&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;button2&quot; VALUE=&quot;Check&Status&quot; onClick=&quot;statusCheck()&quot;><br>

</FORM>

</body>
</html>
 

how about...

***
<html>
<head>
<title>Untitled Document</title>

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

function checkValue(x) {

a = document.myForm.field1.value;
b = document.myForm.field2.value;
c = document.myForm.field3.value;

if ((x=='button1')&&(a!='')) {

if(a>=5 && a<=45) {alert(&quot;'&quot; + a + &quot;' is Right!&quot;);}
else {alert(&quot;'&quot; + a + &quot;' is Wrong! pls enter again&quot;);}
}

if ((x=='button2')&&(b!='')) {

if(b>=-123 && b<=467) {alert(&quot;'&quot; + b + &quot;' is Right!&quot;);}
else {alert(&quot;'&quot; + b + &quot;' is Wrong! pls enter again&quot;);}
}

if ((x=='button3')&&(c!='')) {

if(c>=1 && c<=3) {alert(&quot;'&quot; + c + &quot;' is Right!&quot;);}
else {alert(&quot;'&quot; + c + &quot;' is Wrong! pls enter again&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(name)&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(name)&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(name)&quot;><br>

</FORM>

</body>
</html>
***

hope that helps...

- spewn
 
Hi,

If the function is writtten t his way:
function checkValue(x)

does it means that it only has one argument? How do I make it into three arguments such as the name of the field, lower limit, and upper limit?

Please help. THanks.


 
If you had paid attention to my post, you could see this.

You had it already:
function CheckValue(a,upper,lower)

You should call it like this:
... onClick=&quot;CheckValue(document.myForm.field1.value, document.myForm.field2.value, document.myForm.field3.value)&quot;
 
Thanks for your help. I did try that and I guess what it did was it loops and check all fields instead of only checking a field when user presses the button.

Any clue will be appreciated. Thanks.

 
In fact, I just wonder if it is possible to check only a field in a form instead of checking all fields as each field has each button.

Any clue?
 
I don't understand you.
You did it already - just looked at your first post. You checked that range for several form fields, and not all of them.
In general, this is what you need to do:

<form ... onsubmit=&quot;checkIt(this.txt1)&quot;>
...
<input type=text name=&quot;txt1&quot;>
...

This means that you pass the object of the text field to checkIt() function (&quot;this&quot; is a reference to the currect form). Then you should do the following:
function checkIt(field) {
if (field.value == &quot;&quot;)
{
alert(&quot;it is empty!&quot;);
return false; // don't let the form to submit
}
}

So start from the beginning and explain your case clearly.
Also, I think it's a good idea to get a book about writing functions (in any programming language).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top