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

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}/
commented: nice +14

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

Best regards,
Jola

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.