I need to check the content of a <input>
in which only integers greater than 0 (from 1 to X) can be entered. How can I indicate it in the attribute pattern
? Or should it be checked with a script?
I need to check the content of a <input>
in which only integers greater than 0 (from 1 to X) can be entered. How can I indicate it in the attribute pattern
? Or should it be checked with a script?
There are 2 things that can matter to you. And depending on that will be the solution.
With what everyone told you is enough and more than enough:
Then you have no choice but to use a script.
There are some simpler ones, but the next one that I put can already be classified as far-fetched.
To add another field, you can instantiate another NumberField object by passing it some valid selector.
Note : I added a line of code so that it can be deleted with the delete key as well.
In any case, as @Francisco Romero said, validations should not be left to the client side alone. They are easy to jump.
In this case, just by preventing the execution of the script, and inspecting the field to remove the pattern, the user will be able to send whatever he wants to the server.
I add a simpler solution, and more pleasant for those who use javascript in a classic way. However, there are some differences from the previous solution.
Differences with the previous one - It does not allow pasting acceptable values in the field. - Does not allow valid numbers to be dragged into it. - Accept that the field can be completely deleted. (In both solutions, this behavior can be changed if required.)
Lastly, if you want to use this last solution for multiple fields, I'd recommend using it with event delegation.
To use the pattern attribute , it would be
pattern="[0-9]+"
You can assign the pattern directly in the HTML, which will only write numbers.
There is also the numeric input that only accepts numbers.
And being numbers you can assign a minimum value to these inputs.
In case you wanted to use a
pattern
you could usepattern="^[0-9]+"
so that when you send the form an error appears as if you are entering a wrong value if you enter a negative number.Also, if you want to prevent the user from being able to enter negative values in any way you could use
min="1"
so that from its owninput
it can only select values greater than one.However, the previous step will not prevent a user from the keyboard to enter -15 for example, so you will need a script to control that no one can enter negative numbers in your
input
. For this I have used the functiononkeypress
and I have indicated that if they enter a negative number the number that is entered ininput
it is 1.IMPORTANT: Doing a validation on the client side using
Javascript
is not enough since this type of validation can be manipulated. You should also control it from the server side for more security on your platform.Example:
With html5 you can mark it directly in the input.
Example: integers from 0 to 1000, scale of one in a unit:
More information here .
You can do it with
^[1-9]\d*$
.Forces you to enter only positive numbers greater than zero. Validation is done without the need for scripts.
Example:
You could use two different ways:
Input Type Number
<input type="number" name="points" min="1" max="9" step="1">
Attribute
min
: Specifies the minimum value of an input fieldAttribute
max
: Specifies the maximum value of an input fieldAttribute
step
: Specifies the legal number ranges for an input field.The pattern attribute
<input type="text" pattern="[0-9]{1}" title="Formato: 1 digito" />
<input type="text" pattern="[0-9]{3}" title="Formato: 3 dígitos" />
, that is,001
, etc.InputType example
Number
:Attribute example
pattern
:I added a min of "0" and created a function in Js to validate:
And this is my function: