Rings
Rings

Reputation: 421

Input - Only numbers on mobile browsers

I want a input field where you only can type numbers (no comma, dots, hashtags,...). I have this code in javascript:

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

And the HTML

<input name="pers" type="tel" required="required" id="pers" autocomplete="off" onkeypress="return nummer(event)" size="2" maxlength="2" />

On a desktop browser, this works perfect, but on a mobile browser you can still type a comma, hashtag,...

Someone an idea what the problem is?

Upvotes: 0

Views: 4000

Answers (2)

hari
hari

Reputation: 69

Add this attribute to the pattern="\d+" to the input tag which accepts only numbers as input

Upvotes: 2

user3832769
user3832769

Reputation:

Have you tried using pattern attribute of HTML 5. For your case it will be something like this pattern="{0-9}". In this user will be able to enter alphabets and symbols also but the form will not submit untill the user enters numbers only.

Upvotes: 0

Related Questions