Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex allowing spaces (for a Phone number regex)

I have this regex:

preg_match("#^([0-9]+)$#", $post['telephone'])

who allow only numbers (for a phone number in the French type so 0123456789) but I would like to allow spaces. For example allow this type of string: "01 23 45 67 89".

Can you please help me?

like image 775
Jensen Avatar asked May 17 '11 09:05

Jensen


1 Answers

If you want to have exactly 8 digits, e.g., but still allow for arbitrary number of whitespace characters, the following is great:

^(?:\s*\d){8}$

Or, if you want to allow dashes too:

^(?:\s*-*\s*\d){8}$

like image 131
Erik A. Brandstadmoen Avatar answered Sep 18 '22 00:09

Erik A. Brandstadmoen