2

these are some of indonesia phone number

08xxxxxxxxx (Consist of minimal 11 char length)

08xxxxxxxxxxx (always started with 08)

i found this one is useful

Regex regex = new Regex(@"08[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]");

but, it only support for 12 character, if i change them into the following regex

Regex regex = new Regex(@"08[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]");

it only support for 11 character, how do i make regex for validating to begins with 08 and minimal length is 11?

Community
  • 1
  • 1
Cignitor
  • 891
  • 3
  • 16
  • 36

5 Answers5

6
^08[0-9]{9,}$

The {9,} means "at least 9," but possibly more.

I changed it to 9 to account for the two leading digits (which would add up to 11).

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • the regex still accept the value "08123123123123asd" . it suppose to accept only numeric with 11 character minimum – Cignitor Jan 31 '13 at 04:16
  • Just change to `^08[0-9]{9,}$`. C# may also have a "match whole expression" method (Java does), but I'm not sure – Explosion Pills Jan 31 '13 at 04:18
  • okay, could you change your regex into `^08[0-9]{9,}$` please ? so the other indonesian like me could find the answer easily :) – Cignitor Jan 31 '13 at 04:23
4
08\d{9,10}

Translates to "begins with 08"; Minimum 11 maximum, 12 digits long.

edit: count.

John S.
  • 1,937
  • 2
  • 18
  • 28
  • 1
    Whoa there! Why so many upvotes? This translates to _contains_ a string starting with 08. Also if the `^` had been at the beginning, the min length would have been 13. – CoderOfHonor Jan 31 '13 at 03:36
  • In lue of the additional comments it probably should be either: 08\d{9,10} or 08\d{9,} the author is unclear if max of 12 is desired, also, author doesn't state this is at the beginning of a line, so no ^ or if it as end of line, so no $. – John S. Jan 31 '13 at 03:39
  • Shouldn't there be a `^` at the beginning and a `$` at the end? – CoderOfHonor Jan 31 '13 at 03:42
  • Could be. However the ones that are working do not contain begin/end so adding ^ and/or $ would be a different check that the example given of working ones. So, if the current numbers are embedded in a sentence "Frank's number is 08123456789" Then an additional ^ would not find it, but the author's example would find it. – John S. Jan 31 '13 at 03:44
  • @JohnS. Well shouldn't it at least be changed from 11 to 9? Remember that the prefix of `08` is part of the number total. – CoderOfHonor Jan 31 '13 at 03:48
  • excuse me, that regex accept the char like this `0856912312313asd` – Cignitor Jan 31 '13 at 04:14
1

how about this pattern?

^08\d{9,10}$

this will check for 11 to 12 characters including 08

John Woo
  • 258,903
  • 69
  • 498
  • 492
1

I'd add more complete version, here they are (with digit grouping)

([\[\(])?(?:(\+62)|62|0)\1? ?-? ?8(?!0|4|6)\d(?!0)\d\1? ?-? ?\d{3,4} ?-? ?\d{3,5}(?: ?-? ?\d{3})?\b

It will accept format such

(0811) 123 123

[62] 812 1234567

0812 345 6789

+62856123456789

0878-123-123-123

[+62823] 1234 - 56789 ---> I believe this is the longest phone number as the time of writing

Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
0
((\+62 8\d{2}([ -])|08\d{2}([ -]?)|\+628\d{2})\d{4}(\3\4)\d{2,5})

this will accept most used style like
08xx xxxx xxxx
08xxxxxxxxxxxx
08xx-xxxx-xxxx
+628xxxxxxxxxx
+62 8xx xxxx xxxx
+62 8xx-xxxx-xxxx
also this will check for 11-13 long numbers
sorry for the grammar though

Gal
  • 1