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 "$"?

Link to comment
Share on other sites

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.

Edited by requinix
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.