Asked  6 Months ago    Answers:  5   Viewed   28 times

For <input type="number"> element, maxlength is not working. How can I restrict the maxlength for that number element?



And you can add a max attribute that will specify the highest possible number that you may insert

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

if you add both a max and a min value you can specify the range of allowed values:

<input type="number" min="1" max="999" />

The above will still not stop a user from manually entering a value outside of the specified range. Instead he will be displayed a popup telling him to enter a value within this range upon submitting the form as shown in this screenshot:

enter image description here

Tuesday, June 1, 2021
answered 6 Months ago

I've thrown together a simple demo on jsfiddle here showing how to do this with @font-face:

Opera also has a simple tutorial on using <canvas>, including the text API.


@font-face {
    font-family: 'KulminoituvaRegular';
    src: url('');


var ctx = document.getElementById('c').getContext('2d');
var kitty = new Image();
kitty.src = '';
kitty.onload = function(){
  ctx.drawImage(this, 0,0,this.width, this.height);
  ctx.font         = '68px KulminoituvaRegular';
  ctx.fillStyle = 'orangered';
  ctx.textBaseline = 'top';
  ctx.fillText  ('Keyboard Cat', 0, 270);
Sunday, June 27, 2021
answered 6 Months ago

As of now (30/08/2017), Antoine Thiry's answer seems to be no longer valid in Chrome (my version is 60.0.3112.113). Unfortunately I don't have any other suggestion, other than simulating type="number" with javascript.

Friday, July 2, 2021
answered 5 Months ago

I looked at this and just tried this on my Xoom with the built in browser and FF mobile (with a desktop UA string) and the tel input would seem to work just fine for this.


Friday, July 30, 2021
answered 4 Months ago

You could use Document.execCommand() with the insertText command, which will also fire input events automatically:

const editor = document.getElementById('editor');

editor.oninput = (e) => console.log('Input');

setTimeout(() => {
  document.execCommand('insertText', false, 'Inserted text...nn');
}, 1000);
body {
  display: flex;
  flex-direction: column;
  font-family: monospace;

#editor {
  box-shadow: 0 0 32px 0 rgba(0, 0, 0, .125);
  border-radius: 2px;
  min-height: 64px;
  padding: 16px;
  outline: none;
<div id="editor" contenteditable="true"></div>

However, note that's currently obsolete and even before it was inconsistent across different browser (same as contenteditable):


This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

Wednesday, August 25, 2021
Marko Kevac
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :