Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts

    Question two onlick events, one with a return not working...

    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. :-)

    Code:
    	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;
    		}
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,653 Times in 4,615 Posts
    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:
    Code:
    <input type="text" 
          onkeypress="return numbersonly(this,event) && lessthan100(this);"
    Your function can be written more simply:
    Code:
    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.)

  • #3
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    846
    Thanks
    15
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Old Pedant View Post
    Your function can be written more simply:
    Code:
    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. 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!
    Last edited by dniwebdesign; 05-29-2009 at 03:16 AM.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,688
    Thanks
    80
    Thanked 4,653 Times in 4,615 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •