Accept only specific numbers in an input

Asked

Viewed 170 times

0

I have the following input:

Conta Despesa: 
<input type="number" name="cod_reduzido"> 

However, it should only accept specific numbers (there are 30 numbers that are not in sequence, such as: 3111, 3113, 3130, etc)

1 answer

2


It makes no sense for your input type to be number (with the arrow behavior to increment and decrement being that you are not using sequence. So I switched to type="text", but I added a parameter called pattern validates its input through a Regexp.

Since your case is specific with numbers without any logic between you, you will need to write the same nail ! Follow example with the 3 numbers you passed, just add the rest separated by |(OR).

Conta Despesa: 
<input type="text" name="cod_reduzido" pattern="3111|3113|3130">
<input type="submit" value="validar">

  • there is no logic, are 30 distinct numbers that are the combination of 4 different codes

  • Then this code will supply the need :D

  • yes, it is marked as I accept already, thank you very much

  • but one question, it works on all browsers?

  • 2

    https://caniuse.com/#feat=input-Pattern

Browser other questions tagged

You are not signed in. Login or sign up in order to post.