Jump to content


Photo

Why this reg exp for mobile number does not work?


  • Please log in to reply
4 replies to this topic

#1 mostafatalebi

mostafatalebi

    Advanced Member

  • Members
  • PipPipPip
  • 197 posts

Posted 03 March 2013 - 06:32 PM

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


Visual Effects Programming

Autodesk Maya, Autodesk Mudbox, Nvidia Mental Images Mental Ray, Adobe Photoshop, Adobe Illustrator, Adobe After Effects, CorelDraw

#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,152 posts
  • LocationWA

Posted 03 March 2013 - 07:02 PM

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, 03 March 2013 - 07:03 PM.


#3 mostafatalebi

mostafatalebi

    Advanced Member

  • Members
  • PipPipPip
  • 197 posts

Posted 03 March 2013 - 07:23 PM

No. I mean the former. 09 and not 0 or 9. 

 

I have also another problem. How can I add the length limitation on it? There MUST be ONLY AND ONLY 11 characters of number and not more or less. this is true true format.


Visual Effects Programming

Autodesk Maya, Autodesk Mudbox, Nvidia Mental Images Mental Ray, Adobe Photoshop, Adobe Illustrator, Adobe After Effects, CorelDraw

#4 AyKay47

AyKay47

    Sick!

  • Members
  • PipPipPip
  • 3,287 posts
  • LocationEast Coast, U.S.
  • Age:24

Posted 03 March 2013 - 08:29 PM

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.


Hola!
I'm not going to hold your hand and write the code for you - ain't nobody got time for that!

#5 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 04 March 2013 - 12:27 PM

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.
Keeping it simple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com