I have a field where I need to collect single number
<input type="text" patern="" name="number" required="required" />
Now value can be anything 1-9999 however I have some numbers I don't allow, say
15
21
532
Is there a regex pattern I can use to force form submission to fail if one of this numbers have been entered?
Try this regex:
If you look carefully you will see that I included the number
0
in the disallowed list of15
,21
,532
. The reason for this is that the regex matches any number having 1 to 4 digits, but you only want the range1-9999
.Click the links below for a running demo of this regex.
Regex 101
JS Fiddle
Pretty simple:
isNaN(+num)
fails if the value passed to the function is something other than a real or whole number (e.g., '32 456' fails)Try setting
type="submit"
todisabled
if15
,21
,532
value ofinput type="number"
, usingString.prototype.match()