0

I'm new in html and javascript, coming from C#. Now I have created an input field, which must only accept numeric input. Like so:

<input name="numemp" type="text" id="numemp" value="<%=numberofemp%>" maxlength="10" onkeypress="return isNumberKey(event)" />

And I found this javascript:

function isNumberKey(evt) 
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) 
    {
        return false;
    }
    else 
    {
        return true;
    }      
}

This is not clear for me, I get it that the code checks the pressed key is numeric. And if it is numeric it returns true. But where does the code ignores the input? I only see a return false when this happend...

Cageman
  • 513
  • 3
  • 22

1 Answers1

0

Yes indeed it doesn't ignore the input it just says whether it's a number or not, I think you don't need to use this function :

You only need HTML5 pattern to do it pattern="[0-9]+" and it will be automatically managed:

<input name="numemp" type="text" id="numemp" value="<%=numberofemp%>" maxlength="10" pattern="[0-9]+" />

Take a look at HTML pattern Attribute for further information.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78