Regular Expression checking for Malaysia mobile no.

Malaysia mobile no is in format 601XXXXXXXX (You can find more details from http://www.skmm.gov.my/)

I start learning regular rexpression when I want to do mobile no. validation, basically just refer to two pages, Regular Expression CheatSheet and Regular Expression Tester, a very useful resources to me.

Below is my RegExp test code for mobileNo validation:

^6?01\d{8}$

Let go through the code (Refer to Regular Expression CheatSheet):

  1. This reg. exp. only accept format in (601AAAAAAAA) or (01AAAAAAAA), A is digit from 0 ~ 9.
  2. ‘^601’ indicates that mobile number must start with ‘601’.
  3. Since I want to allow mobile no. in format (01AAAAAAAA), so add a ‘?’ after ‘^6’, it become like this ‘^6?01’. ‘?’ means there is no matching of previous expression or maximum one matching of previous expression, ‘6’ is the one.
  4. In (601AAAAAAAA), notice that A x 8, so the regexp follow by ‘\d{8}’, ‘\d’ match any decimal digit, ‘{8}’ says that any eight decimal digit after that will be match. So the test code is ‘^6?01\d{8}’.
  5. Yeah the story end. ‘^6?01\d{8}’ will be the final code for my validation.
  6. After some time, I found the number ‘60191234567999999’ can pass my validation, oops~~, need to fix the checking.
  7. After browses some regexp example, ‘$’ is found to prevent above error (previously I do not know what is that ‘$’ serve for).
  8. The ‘$’ will make sure ONLY eight decimal digit in the mobile no., so now ‘60191234567999999’ wouldn’t pass my test since there are 13 digits after ‘6019’, but ‘60191234567’ will pass the test.
  9. Ok, story end again.

Anybody has better test code are welcome. 🙂

Reference:

  1. http://regexlib.com/CheatSheet.aspx
  2. http://regexlib.com/RETester.aspx
  3. http://regexlib.com/DisplayPatterns.aspx (Regexp examples)
Advertisements

3 responses to “Regular Expression checking for Malaysia mobile no.

  1. Hi, great post with great explanation on the REGEX.

    However, i have change your REGEX to “^601\d{8,9}$”, due to the new SKMM 11 digits mobile number.

    I removed the “?” for my program to force the user to input the 6.
    I changed “{8,9”} to cater the older (3+7) mobile number, as well as the new (3+8) mobile number

    {8,9} means, minimum 8 chars, maximum 9 chars

    Cheers

    David Ang

  2. ^((\+60(\s|-)?)|0)(((?!0)(?!2)(?!80)(?!81)\d{1,2})[1]?)(\s|-)?\d{2,4}(\s|-)?\d{4}$

    all landline + mobile + IP in these format:

    0XY YYXX XXXX or +60 XY YYXX XXXX

    X must be there, Y may or not may be there depend on which number
    example:
    012 345 6789 or +60 12 345 6789 (normal mobile)
    03 1234 5678 or +60 1234 5678 (selangor fix line)
    088 12 3456 or +60 88 12 3456 (sabah fix line)
    0111 123 4567 or +60 111 123 4567 (new mobile format)

    space does not matter (accept without space too) or can be replace with dash (-)

    do not accept bracket (common style to let people know the number can be omitted for local or STD call)
    do not accept number to singapore (02), brunei (080) or any other unused NDC
    do not accept NSN without trunk prefix (you don’t give people 12-345 6789)
    do not accept country code + NSN without international call prefix too. it will confused people to give full number with country code without the plus sign.
    do not accept input from stupid people who do not know the different between country code and trunk prefix (+6 03 1234 5678)
    do not accept input from stupid people who try to separate subscribe number wrongly (+6011 123 456)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s