954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Regex for telephone number problem

Hi,
im making my new CakePHP project and I've got some problems with validating data, which comes from form. I mean i do know how to add custom rule but i don't know how should regex for 9 numeral telephone number looks like.

function checkPolishNumber($inputValue){

		$regex = 'what to put here?';
		
		if(!empty($inputValue['ask_telefon'])) {
			return preg_match($regex, $inputValue['ask_telefon']);
		}
		else {
			return true;
		}
			
	}

I think it should let through something like this:
+48 111 222 333
111 222 333
111-222-333
111222333

Is it possible to write regex like this?

P.S. Sorry for my creepy english.

Best regards,
Jola

jola.chmiel
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

First make sure what you want. For example, a regex for the last is:

/\d{9}/

So it could be extended to allow space/dashes in place 4 or 8:

/\d{3}[ -]?\d{3}[ -]?\d{3}/

Then, to match also a plus, two digits and a space/dash in front you need something like:

/(\+?\d{2}[ -]?)?\d{3}[ -]?\d{3}[ -]?\d{3}/
pritaeas
Posting Expert
Moderator
5,483 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Thank you. I always find it difficult to write right regex. You explained it really nice. Thanks again. :)

Best regards,
Jola

jola.chmiel
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: