-1

I want this mobile number should be in a format: +911234567 (It should not have any space nor - inbetween numbers)

I'm using this code to validate Mobile Numbers;

if (!preg_match("/((\+63)|0)[.\- ]?9[0-9]{2}[.\- ]?[0-9]{3}[.\- ]?[0-9]{4}/Ui", $_POST['number'], $matches))
{
    // Invalid Number
}

But the regex is throwing the error even if the number is valid. Please help!

user2854563
  • 268
  • 1
  • 11
  • 25
  • 1
    Can you give examples of different values you think should validate but are not? What about values that should not validate, are any of those being handled improperly? Can you explain in words what you think your regex is doing? – Mike Brant Nov 25 '13 at 19:34
  • Out of interest, what's your use case? In most user-input or contact-form situations, you should just record the string as the user types it, since it will be infuriating to them if you reject a valid number (e.g. a number without the international prefix), or if they have used brackets for the traditional "region code" (the first few non-international digits). – halfer Nov 25 '13 at 19:34
  • @Mike Brant: I've no idea what this regex do since I'm regex noob, sorry! I've searched this site and came up with this post: http://stackoverflow.com/a/8904016/2794221 halfer: Yes I'm using this in a form – user2854563 Nov 25 '13 at 19:40
  • @user2854563: OK, use this validation then: `!empty($_POST['number'])` - much easier, and much less likely to lose you a customer. – halfer Nov 25 '13 at 19:42
  • @user2854563 Well then time to learn more about regex. Why just grab a regex and use it if you have no idea what it actually does? What your regex appears to do and what you say you want it to do are two different things. You need to a) understand the real world use case you are trying to solve b) try to cobble together a regex to solve it (if regex is even the best solution). So explain you criteria better. Do you just want a + signed followed by nine digits or is it something more complex than that? – Mike Brant Nov 25 '13 at 19:44
  • Yes I want + sign followed by digits. No matter how many digits should be (It may have even more) but not more then 15 – user2854563 Nov 25 '13 at 19:50
  • 1
    `/^\+\d{,15}$/` <- "+" followed by any number of digits up to 15. – mcrumley Nov 25 '13 at 19:53
  • @mcrumely: It errors on even a valid number – user2854563 Nov 25 '13 at 19:58
  • What is the valid number you are trying? – mcrumley Nov 25 '13 at 20:14

2 Answers2

1

The number that is correct is '+639112345678' this seems to work ok. with the regular expression that you wrote.

The regular expression that you are expecting is this, for this +911234567 number. i've tested it.

/^\+?(?:(?:63)|0)*[.]?9[0-9]{2}[.]?[0-9]{3}[.]?[0-9]{3,4}$/gm

Edited: to not have spaces or - in between.

Zeus
  • 6,386
  • 6
  • 54
  • 89
  • Compilation failed: nothing to repeat at offset 0 – user2854563 Nov 25 '13 at 19:45
  • If the format is "plus sign followed by a fixed number of digits", that looks rather complicated. And it seems to permit dashes and dots as well... `:)`. – halfer Nov 25 '13 at 19:45
  • And if I change the last gm to Ui, it errors: Compilation failed: range out of order in character class at offset 19 – user2854563 Nov 25 '13 at 19:48
  • you dont need 'gm' if you are just validating one number at a time. Try now. updated. – Zeus Nov 25 '13 at 19:50
  • It even goes through if I use this as number: `+911234567asdf` – user2854563 Nov 25 '13 at 19:54
  • which number are you trying? do you know the list of valid numbers? – Zeus Nov 25 '13 at 19:59
  • I'm trying this `+911234567` and if I use non-integer like `+911234567asad` It goes fine – user2854563 Nov 25 '13 at 20:04
  • It should work as per the regular expression. Unless there are spaces in the number that you have entered. look it here http://regexr.com?37bv6 – Zeus Nov 25 '13 at 20:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41900/discussion-between-zeus-and-user2854563) – Zeus Nov 25 '13 at 20:19
1

Try this;

if (!preg_match("/^\+?\d{1,15}$/Ui", $_POST['number'], $matches))
{
    // Invalid Number
}
Imran Omer
  • 701
  • 1
  • 8
  • 20