-1

How do I combine the following, so both are executed? I want to restrict alphanumerics, while allowing enter to be translated to tab on a cell.

function 1 :

var specialKeys = new Array();
specialKeys.push(8,46); //Backspace
function IsNumeric(e) {
    var keyCode = e.which ? e.which : e.keyCode;
    if(keyCode == 9 )return true;
    var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
    return ret;
}

function 2:

function tabE(obj, e) {
        var e = (typeof event != 'undefined') ? window.event : e; // IE : Moz 
        if (e.keyCode == 13) {
            var ele = document.forms[0].elements;
            for (var i = 0; i < ele.length; i++) {
                var q = (i == ele.length - 1) ? 0 : i + 1; // if last element : if any other 
                if (obj == ele[i]) {
                    ele[q].focus();
                    break
                }
            }
            return false;
        }
    }

call 1st function :

onkeypress="return IsNumeric(event) tabE(this,event);"

call 2nd function

onkeypress="return tabE(this,event)"

I tried using "onkeyup" on the enter key to tab function, but it failed.
Anyone have any ideas?

Steven
  • 687
  • 1
  • 10
  • 27
  • 3
    `onkeypress="return IsNumeric(event) && tabE(this,event);"` but you should be setting your handlers from JavaScript, not html attributes – Ruan Mendes Jan 08 '16 at 23:14

2 Answers2

1

The code you have in your inline handler doesn't compile, add && between the two calls. That will make sure the second function doesn't get called if the first returns false

onkeypress="return IsNumeric(event) && tabE(this,event);"

...but you should be setting your handlers from JavaScript, not html attributes

Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Beautiful - thank you very much for your suggestion with handlers - I agree that it makes for cleaner code, I will check out how to conert what I have. – Steven Jan 09 '16 at 05:56
  • this looked nice and simple, I got around to trying it, and it doesn't work in firefox? – Steven Jan 11 '16 at 14:56
  • @Steven "Doesn't work" is not very informative. Make sure you always explain in detail how it "doesn't work". Also, that is a separate problem, and you should create a separate question for it. One question per post makes posts more usable/searchable. – Ruan Mendes Jan 11 '16 at 15:07
  • It does not allow the field to be cycled using the enter key, and it breaks the non numeric script - I also looked into the event listener thing, and I would try that, but my element names are dynamic(added by the user), so I got stuck on how to point to all of the elements – Steven Jan 11 '16 at 15:09
  • @Steven Still not enough information, and it should be a new question – Ruan Mendes Jan 11 '16 at 15:14
  • please enlighten me about the type of information you are looking for...? – Steven Jan 11 '16 at 15:15
  • @Steven In a new question, post an example that runs, where you list the steps to reproduce, along with expected behavior, actual behavior and any error messages. See http://sscce.org – Ruan Mendes Jan 11 '16 at 15:49
0

If you insist on setting your event handlers in the HTML, you can use the comma operator - if you have two expressions separated by a comma (as in (x + 1), (y + 5)), JavaScript will evaluate one of them and return the last one. What makes this so useful in JavaScript is that it's ridiculously easy to turn pretty much anything, including a function call, into an expression (since functions return undefined when a return statement is omitted).

So, in your case:

onkeypress = "return IsNumeric(event), tabE(this,event);"

But as Juan said, this is a little hacky, and it would be cleaner if you used addEventListener() instead.

TheHans255
  • 2,059
  • 1
  • 19
  • 36