Ris Ris - 3 months ago 9
HTML Question

Disable user of multiple same exact numbers in input box

I have an input box here

<input type="text" size="9" maxlength="9" id="my_account" name="my_account" value="" >


And I want to disallow users to enter the same numbers in the box? How can I do this ? Thanks in advance

I don't want them to be able to enter numbers like this

111111111
or
55555555

Answer

@edit If you don't want to let user enter these values as he types you may want to verify only when value equals to 2.

You can listen on keydown event of input element and verify it's actual content and pressed number like this:

var inputNode = document.getElementById('my_account');

inputNode.addEventListener('keydown', (event) => {
  var inputValue = event.key;
  var inputNodeValue = inputNode.value;
  var length = inputNodeValue.length;

  if (length === 1 && inputNodeValue[0] === inputValue) {
    event.preventDefault();
  }
});

If you want to verify on submit, just get value of first character and check if every other is equal to it.

Comments