Search code examples
javascriptoperators

What does || do in ( charCode < 48 || charCode > 57)?


Can anyone tell me what the two lines do here?

( charCode < 48 || charCode > 57))

I guess it means something like "or" or "do both"...

function numberCheck(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode > 31 && ( charCode < 48 || charCode > 57))
{
        document.getElementById("numonly").innerHTML = "Numbers Please!";

        return false;
}
else
{
        document.getElementById("numonly").innerHTML = "";
        return true;
}
}

So what your saying is that the code is looking for all characters except 48-57?


Solution

  • Char codes 48 to 57 represent the number keys 0 - 9

    || means OR

    therefore the expression will evaluate to true for any character that is not a number.