I want to enter only 4 digits between 0001 to 9999 then how to I validate?
I used [0-9]{4} but it contains 0000 also.
I don't think there is a simple regex for a range between 0001 and 9999.
^(0{3}[1-9]|0{2}[1-9]\d|0[1-9]\d{2}|[1-9]\d{3})$
My solution is to split expression for each degree.
0{3}[1-9] = 0001 - 0009
0{2}[1-9]\d = 0010 - 0099
0[1-9]\d{2} = 0100 - 0999
[1-9]\d{3} = 1000 - 9999
You can play with this regex online https://regex101.com/r/TjzoEh/1
Use pattern attribute of text input to validate your regex.
<input type="text" pattern="^(0{3}[1-9]|0{2}[1-9]\d|0[1-9]\d{2}|[1-9]\d{3})$" title="Four digits code">
Inspired by https://www.regular-expressions.info/numericranges.html
for set your length, you must use maxlength attribute like this:
<input type="text" maxlength="4"/>
and for check number type, you can use Regular Expressions or Number.isInteger() method.
For first way, you can use this pattern and find any non-digit in your input
// Find a non-digit character
let reg = /\D/;
and finally, use it:
reg.test({{your value}})
For second way, you have to use eval() method in the Number.isInteger() method, for example:
Number.isInteger(eval({{ your value }}))
NOTE: if you use type="number" attribute in input, use don't have to use regex or any js methods.