Vg. Vg. - 7 months ago 44
Javascript Question

Regex for age validation that accepts an age between 0-200 using Javascript only

I want a regex that allows users to enter only numbers between 0 and 200.
I've tried this but it doesn't work:

var age_regex=/^\S[0-9]{0,3}$/;

Answer

Instead of regex, you can compare numerical value itself:

var ageValue = 50; // get the input age here
var ageNumericVal = +ageValue;
if (ageNumericVal < 0 || ageNumericVal > 200) {
  // invalid
}