0

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

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by jola.chmiel
2

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}/

Edited by pritaeas: n/a

Votes + Comments
nice
0

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

Best regards,
Jola

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.