Jump to content

Why this reg exp for mobile number does not work?


mostafatalebi

Recommended Posts

Hello everybody

 

I need mobile number validation. Number "09" is fixed at the beginning. And it is followed by other numbers from 0 to 9.

When I use this reg exp: "/^([09]+[0-9])$/"      It fails

 

But when I use this one:   "/^([09]+[0-9])/"      it works

 

 

 

 

What's the problem? Is it okay to use it without "$"?

You need the $.

 

Your expression says "at least one of [09] and then only one [0-9]". Does that sound right?

 

[edit] And when you say "09 is fixed" do you mean literally 09 or either 0 or 9? Because your expression says the latter.

A simple regex should be fine:

 

 

<?php
$pattern = "~^09\d{9}$~";
?>

 

While you can't guarantee that all numbers that run through this regex will be mobile numbers and not land lines, you can ensure that they conform to the pattern that you specify.

You might also want to read up on regular expressions, so that you know what each character and each element of that RegExp means. Should help you avoid mistakes like this in the future. ;)

 

Also, for PHP I generally recommend using \z instead of $ as the "end of string" match, seeing as the dollar sign really means "end of string, or the last newline before the end of the string".

An alternative to using \z is using the D modifier with the dollar sign.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.