how to write pattern for input type="text" (it can't be number and the validation can't be with JS) that allow me to enter only numbers, min:1 and max:5000?
<input type="text" name="someName" id="someId" required pattern=""/>
addEventListener('change', function(e) { var num = parseInt(this. value, 10), min = 0, max = 100; if (isNaN(num)) { this. value = ""; return; } this.
To limit an HTML input box to accept numeric input, use the <input type="number">. With this, you will get a numeric input field. After limiting the input box to number, if a user enters text and press submit button, then the following can be seen “Please enter a number.”
Definition and Usage The min attribute specifies the minimum value (time) for a time field. Tip: Use the min attribute together with the max attribute to create a range of legal values. Tip: To set or return the value of the max attribute, use the max property.
The pattern attribute specifies a regular expression that the <input> element's value is checked against on form submission. Note: The pattern attribute works with the following input types: text, date, search, url, tel, email, and password. Tip: Use the global title attribute to describe the pattern to help the user.
Here you go - not an input with type="number"
and no JS:
<input type="text" name="someName" id="someId" required="required"
pattern="(5000|([1-4][0-9][0-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9])|[1-9])"/>
The basic pattern is to match 5000 or 4-digit number or 3-digit number or 2-digit number or non-zero 1-digit number.
If you can accept 0, the pattern can be even simpler:
(5000|([1-4]?[0-9]?[0-9]?[0-9]?))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With