Phone number validation

Ask about a PHP problem here.
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Phone number validation

Post by Fidbeck »

[syntax=php]
if (!empty($contact_phone)) {
if (!preg_match('#^9[1236]{1}[0-9]{7}$#', $number)) {
$errors[] = "Phone number must be 9 numbers long and must start with 91, 92, 93 or 96.";
}
}
[/syntax]

so let me see if I understood that.
the first digit is 9 the second can be 1, 2, 3 or 6 that {1} is basically saying that is only one option.
the rest the numbers can be from 0 to 9 and is total of 7
right?

Let's complicate things up a bit
What about land phones
in general the first 3 numbers tell you the place you caliing and they all start with the number 2, but there are two who only have 2 digits that tell you the zone.
like this
[syntax=text]21 - indicativo de Lisboa
22 - indicativo do Porto
231 - indicativo da Mealhada
232 - indicativo de Viseu
233 - indicativo da Figueira da Foz
234 - indicativo de Aveiro
235 - indicativo de Arganil
236 - indicativo de Pombal
238 - indicativo de Seia
239 - indicativo de Coimbra
241 - indicativo de Abrantes
242 - indicativo de Ponte de Sor
243 - indicativo de Santarém
244 - indicativo de Leiria
245 - indicativo de Portalegre
249 - indicativo de Torres Novas
251 - indicativo de Valença
252 - indicativo de Vila Nova de Famalicão
253 - indicativo de Braga
254 - indicativo do Peso da Régua
255 - indicativo de Penafiel
256 - indicativo de São João da Madeira
258 - indicativo de Viana do Castelo
259 - indicativo de Vila Real
261 - indicativo de Torres Vedras
262 - indicativo das Caldas da Rainha
263 - indicativo de Vila Franca de Xira
265 - indicativo de Setúbal
266 - indicativo de Évora
268 - indicativo de Estremoz
269 - indicativo de Santiago do Cacém
271 - indicativo da Guarda
272 - indicativo de Castelo Branco
273 - indicativo de Bragança
274 - indicativo de Proença-a-Nova
275 - indicativo da Covilhã
276 - indicativo de Chaves
277 - indicativo de Idanha-a-Nova
278 - indicativo de Mirandela
279 - indicativo de Torre de Moncorvo
281 - indicativo de Tavira
282 - indicativo de Portimão
283 - indicativo de Odemira
284 - indicativo de Beja
285 - indicativo de Moura
286 - indicativo de Castro Verde
289 - indicativo de Faro
291 - indicativo do Funchal
292 - indicativo da Horta
295 - indicativo de Angra do Heroísmo
296 - indicativo de Ponta Delgada[/syntax]
http://telefone.awardspace.info/

Using the expression Jacek wrote
it could be done like this

my doubt is that
if here (!preg_match('#^9[1236]{1}[0-9]{7}$#', $number)) I use (!preg_match('#^2[3132...]{2}[0-9]{7}$#', $number)) that 3132 won't be considered as 3 1 3 2 instead of 31, 32
Well this can be considered as being a rule and wjat about the exception?
well I guess this one is easy
(!preg_match('#^2[12]{1}[0-9]{7}$#', $number))

EDIT1: thinking better if I use
(!preg_match('#^2[3132...]{2}... will it pick 31 32 right?
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: Phone number validation

Post by EcazS »

No, it would pick 3 or 1 or 2.
You would need to use the OR operator between the numbers, 31|32|xx|etc
Post Reply