Click here to Skip to main content
15,125,299 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how do i create a javascript code for an input to be postive numbers only and have a max  value  of 20

What I have tried:

ive been stuck on how to begin so haven't really tried anything, do I create a regex for this?
Updated 14-Apr-21 6:51am

Why use Javascript at all? HTML will do the job:
<input type="number" step="1" min="0" max="20" ... />
<input type="number"> - HTML: HyperText Markup Language | MDN[^]
Start here:[^]

While you could do it with a regex, that's probably the wrong tool - it's fine for strings, but it has no concept of numbers so "greater than" and "less than" don't exist - you would have to do a really dull regex that listed all positive numbers below 21 as separate strings - and while that's OK for a small maximum, it gets really dull when you have to change that.
Rather than repeatedly posting these basic questions, you would be better spending your time at JavaScript Tutorial[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900