...

View Full Version : two onlick events, one with a return not working...



dniwebdesign
05-29-2009, 01:41 AM
I have this form entry <input type="text" onkeypress="return numbersonly(this,event); lessthan100(this);" name="discount_value" id="discount_value" value="" style="width: 40%;" maxlength="3" class="input-symbol-parent"> which calls two javascript functions. The first one works without a hitch... no letters can be entered into the field. However the next function (which at present time, just contains an alert) doesn't fire and I get no alert.

I need to check if it's a number first. Below is the number checker. :-)


function iscontrolkey(key) {
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ||
(key==63232) || (key==63233) || (key==63234) || (key==63235) || (key==63272)) return true;
else return false;
}

function numbersonly(myfield, e) {
var key;
var keychar;
if (window.event) key = window.event.keyCode;
else if (e) key = e.which;
else return true;
keychar = String.fromCharCode(key);
if (iscontrolkey(key)) return true;
// numbers or decimal
else if ((("0123456789.").indexOf(keychar) > -1)) return true;
else return false;
}

Old Pedant
05-29-2009, 01:58 AM
the keyword return--in every language I know of--means "return RIGHT NOW, do not do ANY other code past this point".

So, of course all that happens is that the onkeypress event handler *QUITS* upon the return of the numbersonly() call.

Try this:


<input type="text"
onkeypress="return numbersonly(this,event) && lessthan100(this);"

Your function can be written more simply:


function iscontrolkey(key)
{
return (key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ||
(key==63232) || (key==63233) || (key==63234) || (key==63235) || (key==63272)
}
(and you don't really need any of those parentheses, but yeah, I often put them in, too.)

dniwebdesign
05-29-2009, 02:08 AM
Your function can be written more simply:


function iscontrolkey(key)
{
return (key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ||
(key==63232) || (key==63233) || (key==63234) || (key==63235) || (key==63272)
}
(and you don't really need any of those parentheses, but yeah, I often put them in, too.)

However, then my script debug thing here in FireFox gives a warning stating: function iscontrolkey does not always return a value. :o Just getting rid of the warning was all that last "else return false" value did. ;-)

I knew what returned meant, I was just unsure of how to do both with the one with the return running first. :)

Thanks for your help!

Old Pedant
05-29-2009, 02:21 AM
Which mean the "script debug thing" in FireFox is braindead.

There's no possible way to *NOT* return a value with the code like that.

Shame on them.

Notice that there is no "if" in there. You sure your code didn't use an "if"?

I should have noticed your number of posts and figured you knew what "return" meant, sorry. Yeah, the && means that if the first function returns true then JS *must* call the second one, to find out what the final result is.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum