giraffemedia Posted November 9, 2011 Share Posted November 9, 2011 Hello, I'm trying to put together a regex that validates a two number input. I want a person to enter their age but it has to be 18 or over and up to 99. I've tried using ^([1-9][8-9])$ but obviously that only validates 18, 19, 28, 29, 38, 39 etc missing out the other numbers. Can anyone help please? Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/ Share on other sites More sharing options...
trq Posted November 9, 2011 Share Posted November 9, 2011 You should firstly validate the data is a number using regex, then just use a simple expression to test that number is greater than 18. Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286519 Share on other sites More sharing options...
giraffemedia Posted November 9, 2011 Author Share Posted November 9, 2011 Hi Thorpe, It's for a Joomla component and I haven't got a lot of control over other aspects or filtering. I need a php regex that allows any number from 18-99 but nothing else. James Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286521 Share on other sites More sharing options...
JAY6390 Posted November 9, 2011 Share Posted November 9, 2011 Why would you use regex for this? Surely it's easier to just use regular php $age = (int) $_POST['age']; if($age >= 18 && $age <= 99) { // Age is correct } else { // Age is incorrect } Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286524 Share on other sites More sharing options...
JAY6390 Posted November 9, 2011 Share Posted November 9, 2011 Also, if you really want to do it via regex...you could use this, although personally I find it a bit overkill ~^(1[89]|[2-9][0-9])$~ Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286525 Share on other sites More sharing options...
giraffemedia Posted November 9, 2011 Author Share Posted November 9, 2011 Why would you use regex for this? Surely it's easier to just use regular php $age = (int) $_POST['age']; if($age >= 18 && $age <= 99) { // Age is correct } else { // Age is incorrect } Hi Jay, if you read my 2nd post you'll see I can only enter a regex for validation. Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286526 Share on other sites More sharing options...
JAY6390 Posted November 9, 2011 Share Posted November 9, 2011 Ah OK, I skipped that post sorry. my regex above should work Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286527 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 JAY6390's example will only be true for values less than 100. ^(?:[1-9]\d{2,}+|[2-9]\d|1[89])$ The first section checks for 1-9 followed by 2 or more digits. This covers all numbers greater than 99 The second checks for a number between 2-9 followed by a single number. This covers all two digit numbers over 19 The final checks for a 1 followed by either an 8 or a 9. This covers 18 and 19. The RegEx doesn't allow leading 0's, but this could be checked fairly easily by adding something like 0*+ immediately after the opening ^. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286698 Share on other sites More sharing options...
JAY6390 Posted November 9, 2011 Share Posted November 9, 2011 I want a person to enter their age but it has to be 18 or over and up to 99 This is why my solution stops at 99 Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286699 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 Guess I should read. Oh well... knowledge is power... MY POST IS JUSTIFIED! Quote Link to comment https://forums.phpfreaks.com/topic/250755-over-18-regex/#findComment-1286711 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.