13

I have this simple html:

<input type="text" 
       placeholder="onlynumbers" 
       name="onlynumbers" 
       pattern="\d{1,5}"  
       maxlength="5">

I need to restrict the number of characters to 5. It works. But I still can type other characters than numbers. I have tried various pattern, for example:

pattern = "[0-9]"

But it is still possible to type other characters than digits.

If I change the type attribute to number, then only digits are accepted in the input field but the maxlength attribute doesn't work. I can enter as many digits as I want. I heard this was a bug in Chrome. Is that the problem?

Is there a cross-browser way to fulfil these requirements:

An input field (text or number, no matters) that only accept digits and doesn't let the user enter more than 5 digits.

5
  • pattern does not prevent the user from entering other characters. To prevent, you need to use JavaScript. Commented Dec 12, 2017 at 9:35
  • The correct attributes for a number input are min & max. Use max="99999" Also the pattern attribute is implicitly a whole string match. So pattern="[0-9]" means "only one digit" Commented Dec 12, 2017 at 9:36
  • 1
    Please search thoroughly before posting. More on searching here. Commented Dec 12, 2017 at 9:36
  • 2
    use type='number' in input element Commented Dec 12, 2017 at 9:36
  • <input type="number"> may be? Commented Dec 12, 2017 at 9:37

5 Answers 5

33
<input type="text" name="country_code" pattern="[0-9]+" title="please enter number only" required="required">
Sign up to request clarification or add additional context in comments.

3 Comments

This is the right answer. type="number" is for amounts. That's why most browsers display spinner controls. If you're asking for i.e. a house number, it's not an amount, you just expect numbers.
Another use case where type="text" is needed.: the number is an identifier and may start with a "0".
actually better would be use type="tel" as from mozilla docs developer.mozilla.org/en-US/docs/Web/HTML/Element/input/tel
9

Use max instead of maxlength with input type="number". For example, to specify max length of 5 digits, use

<input type="number" max="99999" />

Comments

3

You can use the Below jQuery code to check with regex (For old browsers that does not support the type=number input pattern)

$(function(){
  $("input[name='onlynumbers']").on('input', function (e) {
    $(this).val($(this).val().replace(/[^0-9]/g, ''));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" placeholder="onlynumbers" name="onlynumbers"   maxlength="5">

Comments

1

Use This Code snippets to restrict input to only 5 digits and restrict to letters with the help of JavaScript make thing easier instead of JQuery

function isNumber(evt)
		 {
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true; }
<!DOCTYPE html>
<html>
<head>
	<title>Restrict Number</title>
	<script src="number.js"></script>
</head>
<body>
<form>
	Number:<input type="text" maxlength="5" onkeypress=" return isNumber(event)"/>
</form>
</body>
</html>

Comments

-4

Why not you use number input type?

<input type="number" name="onluNumbers" min="1" max="99999">

2 Comments

because sometimes you need the output to be string, that means leading zeros are important
@Johan idk about you... but theInput.value does seem to be a string (retaining all leading zeros too)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.