UnsuitableBadger Posted November 17, 2009 Share Posted November 17, 2009 Hi guys I'm having problems with regex. I'm entering an international phone number in the following format: +27116781234 I need a regex pattern that matches the "+" sign at the beginning and then 11 digits. My biggest problem is that I can't match the "+" sign no matter what i try. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/181841-solved-regex-match/ Share on other sites More sharing options...
Daniel0 Posted November 17, 2009 Share Posted November 17, 2009 You need to escape it using a backslash like this: \+ The plus sign is a meta character in regular expressions, specifically it's a quantifier matching the previous subexpression greedily at least once. Quote Link to comment https://forums.phpfreaks.com/topic/181841-solved-regex-match/#findComment-959037 Share on other sites More sharing options...
UnsuitableBadger Posted November 17, 2009 Author Share Posted November 17, 2009 I tried the \+ but it doesn't like it. What i'm doing is using ajax on a registration form to check whether the phone number is in the correct format. Following is my ajax response code that is triggered in the onblur event of the phone number field: $value = $_REQUEST['value']; // phone number entered in field $pattern = "/\+/"; // regex pattern if ($value != "") { if (preg_match($pattern, $value) == false) echo "<font color='#ff0000'>invalid format</font>"; else echo "<font color='#00ff00'>&radic</font>"; } Quote Link to comment https://forums.phpfreaks.com/topic/181841-solved-regex-match/#findComment-959042 Share on other sites More sharing options...
cags Posted November 17, 2009 Share Posted November 17, 2009 Daniel0 was just giving an example of how to match the plus (by escaping it), not how to match your entire pattern. To match something of the format "+ followed by 11 digits" you would use something like... $pattern = "#\+[0-9]{11}#"; That will match that pattern in another string. If you are using it to validate a string as being of that format and only of that format only you can use something more like... $pattern = "#^\+[0-9]{11}$#"; Quote Link to comment https://forums.phpfreaks.com/topic/181841-solved-regex-match/#findComment-959160 Share on other sites More sharing options...
UnsuitableBadger Posted November 18, 2009 Author Share Posted November 18, 2009 I'm aware he wasn't giving me the whole pattern but that's not the issue. Just trying to match the "+" sign is giving issues no matter what i try. Quote Link to comment https://forums.phpfreaks.com/topic/181841-solved-regex-match/#findComment-959917 Share on other sites More sharing options...
UnsuitableBadger Posted November 18, 2009 Author Share Posted November 18, 2009 ahhh found the issue, it's not passing the "+" through the POST. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/181841-solved-regex-match/#findComment-959919 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.