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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using multiple functions with onchange

Status
Not open for further replies.

xsw1971

Programmer
Jun 21, 2001
153
US
I have read in the forum several ways to handle calling 2 functions but nothing works for me (it just simply be syntax).

I have a function that verifies something was entered in a form field:
Code:
function hasValue(form_obj) {
  var myVar = form_obj.value;
  if (myVar == "") {
    alert("This field cannot be left emtpy.");
    return false;
    form_obj.focus();
  }
}

I have another function that verifies the entry was a number if the field has data entered:
Code:
function isNumber(form_obj) {
  var myValue = form_obj.value;
  if (myValue != "")
  {
    if (isNaN(myValue) || (myValue < 1)) {
      alert(&quot;Please enter an integer greater than 1&quot;);
      return false;
      form_obj.focus();
    }
  }
}

I don't think I can combine them because I have some form fields that just use 1 function and other fields that just use the other function. Now I have a field which needs to use both. When I use
Code:
onchange=&quot;return hasValue(this)&quot;
OR
Code:
onchange=&quot;return isNumber(this)&quot;
each works great.

I've tried each of the following with no luck:
-
Code:
onchange=&quot;return hasValue(this); return isNumber(this)&quot;
-
Code:
onchange=&quot;return hasValue(this); isNumber(this)&quot;
-
Code:
onchange=&quot;a=hasValue(this); b=isNumber(this); return a && b&quot;

JS is so frustrating! Please help!

Thanks,
Jen
 
combine them and use a onBlur. with the onChange if the user never does anything to the field the empty value never gets checked
<html>
<head>
<script>
function hasValueandNum(form_obj) {
var myVar = form_obj.value;
if (myVar == &quot;&quot;) {
alert(&quot;This field cannot be left emtpy.&quot;);
return false;
form_obj.focus();
}
if (isNaN(myVar) || (myVar < 1)) {
alert(&quot;Please enter an integer greater than 1&quot;);
return false;
form_obj.focus();
}
}


</script
</head>
<body>
<form>
<input type=&quot;test&quot; onBlur=&quot;return hasValueandNum(this);&quot;>
</form>
</body>
</html>

Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Or to combine the two statements you had, try this:
Code:
<input type=&quot;test&quot; onBlur=&quot;return (hasValue(this) && isNumber(this))&quot;>

What this will do is return both booleans back to your calls, then use a logical and statement which will only be true if they are both true, which then gets returned the way you want :)

Et Voila!
Isn't vacation fun :)
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
FAQ FAQ20-2863
= new Forums.Posting.General.GettingAnswers()
 
Thanks for the replies!

Tawrn, I prefer your solution because I don't want to combine the functions (other fields are using 1 or the other, not both). However, it still doesn't work. I used your exact syntax and what happens is that it will not let me leave the field if it's empty. When I put a 0 or a negative number in the field it just continues on, like it's not seeing the inNumber() function.

For the sake of getting this project done, I did use onpnt's suggestion and it works well (thanks onpnt!). My only complaint is now I have 3 functions in my header: hasValue(), isNumber(), and hasValueAndNum() which I think think defeats the purpose of code reusability. I would still like to get it to use the 2 functions that already exist...

Thanks,
Jen
 
hmm.
I wanted to ask about these lines here
if (isNaN(myValue) || (myValue < 1)) {
alert(&quot;Please enter an integer greater than 1&quot;);
here you are asking if a number and if value is less than 1
do you mean > 1 greater than??

Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top