Disable a user with multiple identical exact numbers in the input field

I have an input field here

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

And I want to prevent users from entering the same numbers in the field? How can i do this? thanks in advance

I do not want them to be able to enter numbers like this

111111111 
or
55555555
+4
source share
3 answers

@edit If you do not want the user to enter these values ​​as types, you can only check when the value is 2.

You can listen to the keydown event of the input element and check its actual contents and the number pressed as follows:

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();
  }
});

submit, , .

+1

, :

var validator = /\b(\d)\1+\b/

console.log(validator.test('111')) // true
console.log(validator.test('123')) // false
console.log(validator.test('121')) // false
console.log(validator.test('112')) // false
Hide result
+2

:

<input type="text" size="9" maxlength="9" id="my_account" name="my_account" value="" pattern="^(?!(\d)\1{8}).*">
Hide result

:

  • , , , .* \d*
  • " ". , , "3 ", ^(?!\d*(\d)\1{2,}).*
  • - , : ^(?!(\d)\1*$).*

" 3 , ":

<input type="text" size="9" maxlength="9" id="my_account" name="my_account" value="" pattern="^(?!\d*(\d)\1{2,})\d*">
Hide result

" , ":

<input type="text" size="9" maxlength="9" id="my_account" name="my_account" value="" pattern="^(?!(\d)\1*$)\d*">
Hide result
+1
source

Source: https://habr.com/ru/post/1651101/


All Articles