-8

I'm still trying to find my way around Javascript regular expressions, and I have not been able to deduce how to write this regex I need.

Please I need a Javascript regex basically for age. I want to allow anybody between ages 18 and 140. With 18 being allowed and 140 being allowed. I want a regex that basically accepts 18,19,20,21,...138,139,140. and ONLY THIS.

I'd appreciate any help.

P.S. I hope I just get a simple answer without Stack Overflow closing the question down and saying 'duplicate' or this or that... if not this might be my very last question on stack overflow, because sadly, it SEEMS like Stack Overflow is making it harder to ask a simple question. The point they want you to do a TON OF RESEARCH...or at least a lot, before you ask a question. Even though many times we ask questions precisely because we DON'T have the the time to do a lot of research. :: sigh ::

Hideyoshi
  • 81
  • 3
  • 12
  • 3
    Why would you use regex for something like this? It seems like a trivial issue that doesn't require it, but rather some basic logical operators.. What have you tried? – cbll Mar 28 '17 at 12:26
  • Why do you want a regex and not just `var test = parseInt(input); if (test < 18 || test > 140) { errorHandler(); }`? – Psi Mar 28 '17 at 12:26
  • 3
    If you detail what you have tried, it is much less likely that people will vote to close your question. – forgivenson Mar 28 '17 at 12:27
  • 1
    if the age is coming from a form you could use [input type number](https://www.w3schools.com/jsref/dom_obj_number.asp) – Ionut Mar 28 '17 at 12:30
  • [This SO question](http://stackoverflow.com/questions/22130429) -> [This article](http://code.activestate.com/recipes/534137-generate-a-regular-expression-to-match-an-arbitrar/) -> [Github](https://github.com/dimka665/range-regex) -> [Your answer for many other identical questions](https://ideone.com/Rpowdb). Research done in 13 minutes. – Wiktor Stribiżew Mar 28 '17 at 12:37
  • If you google for [`regex range generator`](https://www.google.com/#q=regex+range+generator&*) you can find [Regex Numeric Range Generator](http://gamon.webfactional.com/regexnumericrangegenerator/) which generates a pattern similar [`^(?:1[89]|[2-9][0-9]|1[0-3][0-9]|140)$`](https://regex101.com/r/wO9GRr/1). – bobble bubble Mar 28 '17 at 16:25

2 Answers2

3

When Answering your question, I am not the person to ask why you need regex for this.

And the regex you want is

/^(1[89]|[2-9][0-9]|1([0-3][0-9]|40))$/

Sample

var age=/^(1[89]|[2-9][0-9]|1([0-3][0-9]|40))$/;

console.log(age.test(18));
console.log(age.test(140));
console.log(age.test(12));
console.log(age.test(142));

But, you can simply use the following code to test

if(age>=18 && age<=140)

That is

function test(age){
  return age>=18 && age<=140;
}

console.log(test(18));
console.log(test(140));
console.log(test(12));
console.log(test(142));
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • Thanks a million. This regex worked perfectly...tested various numbers. Only the range I asked for worked. Cheers. – Hideyoshi Mar 28 '17 at 13:34
1

Try this:

/^(1[89]|[2-9]\d|1[0-3]\d|140)$/

Let me know if that does what you need.

Matthew
  • 462
  • 8
  • 20