Jump to content

[SOLVED] Regex match "+"


UnsuitableBadger

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/181841-solved-regex-match/
Share on other sites

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>";
}

Link to comment
https://forums.phpfreaks.com/topic/181841-solved-regex-match/#findComment-959042
Share on other sites

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}$#";

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/181841-solved-regex-match/#findComment-959160
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.