RegEx Validation for a 01<x<32
Moderator: General Moderators
RegEx Validation for a 01<x<32
Hi I'm trying to validate a number that comes within this range
01 < x < 32
x should always be a 2 digit number.
Can anyone help me with the regEx expression that will help me validate X
Thanks,
Bitz
01 < x < 32
x should always be a 2 digit number.
Can anyone help me with the regEx expression that will help me validate X
Thanks,
Bitz
-
blackbeard
- Forum Contributor
- Posts: 123
- Joined: Thu Aug 03, 2006 6:20 pm
If I understand you right, you need to make sure that string $x fits a particular format (two numbers), and then you need to make sure that it's value falls between 1 and 32. If so, you can try this:
Code: Select all
if (preg_match("[0-9]{2}", $x)) // test $x to make sure it's two digits
if (intval($x) >= 1 && intval($x) <=32) // it's between 1 and 32
echo $x."It's a match";
else
echo "Not a match";RegEx Validation for a 01<x<32
Hi BlackBeard,
Thanks for your response.
Actually what i was looking for is that the RegExpression determines whether X a 2 digit number or not and also if it falls within the specified range.
Is there a way to do it ?
Thanks
Bitz
Thanks for your response.
Actually what i was looking for is that the RegExpression determines whether X a 2 digit number or not and also if it falls within the specified range.
Is there a way to do it ?
Thanks
Bitz
untested:
Code: Select all
preg_match('/^(?[012]\d|3[012])$/', $x)-
blackbeard
- Forum Contributor
- Posts: 123
- Joined: Thu Aug 03, 2006 6:20 pm
Maybe this:
Code: Select all
if (preg_match("^[0-3][0-2]", $x)) { // test $x to make sure the first digit is 0 to 3, the second is 0 to 2
/* do whatever */
}-
blackbeard
- Forum Contributor
- Posts: 123
- Joined: Thu Aug 03, 2006 6:20 pm
Ok, tested now and expressed as a SimpleTest test method:
Code: Select all
function testPregToMatchTwoDigitNumberLessThan32() {
$regex = '/^(?:0[1-9]|[12]\d|3[012])$/';
foreach(range(1,32) as $i) {
$this->assertTrue(preg_match($regex,sprintf('%02d',$i)));
}
$this->assertFalse(preg_match($regex,'001'));
$this->assertFalse(preg_match($regex,'00'));
$this->assertFalse(preg_match($regex,'33'));
$this->assertFalse(preg_match($regex,32.1));
$this->assertFalse(preg_match($regex,' 08'));
$this->assertFalse(preg_match($regex,'18 '));
$this->assertFalse(preg_match($regex,'foo'));
}RegEx Validation for a 01<x<32
Hi,
Sweatje,
Thanks for giving me the solution. This is exactly what i was looking for.
Could you please take some more time out and tell me what is happening here? Is it one of those lookbehind and lookahead stuff ?
Thanks
Bitz
Sweatje,
Thanks for giving me the solution. This is exactly what i was looking for.
Could you please take some more time out and tell me what is happening here? Is it one of those lookbehind and lookahead stuff ?
Thanks
Bitz
Re: RegEx Validation for a 01<x<32
Integers dont have zero padding so you must be looking for a string. Is that correct? Are "01" and "32" fixed values.bitz21 wrote:Hi I'm trying to validate a number that comes within this range
01 < x < 32
x should always be a 2 digit number.
Can anyone help me with the regEx expression that will help me validate X
Thanks,
Bitz
If 01 and 32 are included:
If not:
This will only match said values and is also as efficient as it gets
Code: Select all
0[1-9]|[12]\d|3[0-2]Code: Select all
0[2-9]|[12]\d|3[01]