Welcome to WebmasterWorld Guest from 54.197.171.28

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Need help on this REGEXP

   
2:42 pm on Oct 19, 2011 (gmt 0)

5+ Year Member



Hi all,
I have a small code snippet that validates fixed national telephone numbers. Our telephone code always begins by 22 followed eventually by a space. The next 2 figures are between the range of 21 and 30 but exclude figures 28 and 29. At the end of a valid number are always 4 figures between 0 and 9. There can be spaces between two consecutive figures.

I have the next code snippet for validation:

<?php
if(isset($_POST['tel'])){
$telephone = htmlentities($_POST['telephone']);

if(preg_match("#^22[ ]?([21-27]|30)([ ]?[0-9]{2}){2}$#", $telephone)){
echo $telephone ." is a valid <strong>number</strong>";

}else{
echo $telephone ." is not a valid number, please correct it";
}
}
?>

If i enter 22 30 22 59 (with or without space) the number is found to be valid, however if i enter 22 21 10 57 with or without spaces the number is rejected. What could be wrong, please?

Your help is more than appreciated
6:39 pm on Oct 19, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



I assume "eventually" means (in English) "optionally". (I know languages where this form occurs, though I gotta say they don't match your name.) Also that "figures" means "group of digits", not "(individual) digits".

[21-27] does not mean
"any two-digit number in the set 21 22 23 24 25 26 27"
It means
"any one-digit number in the set 2 1-2 7-- i.e. 1 2 7"

Similarly
([ ]?[0-9]{2}){2}
does not mean "four numerals, optionally preceded by a space".
It means "two numerals, optionally preceded by a space, and then the identical group again".

It is safer to get rid of the spaces beforehand. Either design your input to ignore them (one way is to have three separate boxes, each of which skips to the next box as soon as the user has entered the right number of digits), or strip them out before you run any code.

I make it

22(2[1-7]|30)[0-9]{4}
or
22(2[1-7]|30)\d{4} (if your setup supports the \d notation)

although \d\d\d\d may be just as fast. If you need to capture, put the whole thing in parentheses as (for example)

(22(2[1-7]|30)[0-9]{4})

[edited by: lucy24 at 7:05 pm (utc) on Oct 19, 2011]

6:57 pm on Oct 19, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Which African country is this for?
7:24 pm on Oct 20, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Although I agree with Lucy - if you don't want to strip spaces:
/^22\ ?(2[1-7]|30)(\ ?\d{2}){2}$/

However:

([ ]?[0-9]{2}){2}
...
It means "two numerals, optionally preceded by a space, and then the identical group again".

I have never found this to be the case when working with PCRE in PHP. I find that it allows the second match to differ from the first.
8:38 am on Oct 21, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



I have never found this to be the case when working with PCRE in PHP. I find that it allows the second match to differ from the first.

I think you're right, and not just for php. I now remember doing analogous things in text editing. The two pieces only match if you say explicitly ([ ]?[0-9]{2})\1 (or whatever your dialect requires for the inline capture).