3 JavaScript Regular Expression for Malaysia Data Format

Posted on the September 17th, 2008 under JavaScript, Programming, Q&A, Tutorial by Zaki

Question

Can you share with us the proper Regular Expression (RegExp) for Malaysia’s data format as the following:

  • Identification Card
  • Postcode
  • Phone Number

Answer

Here the answer for above question.

a. Identification Card

/^\d{6}-?\d{2}-?\d{4}$/ allow us to detect Malaysian Identification Card data entry with and without the hyphen (”-”), e.g:

var ic = ["830214-14-5065", "830214145065", "8302-14-5065"];
for(var i = 0; i < ic.length; i++) {
    if(ic[i].match(/^\d{6}-?\d{2}-?\d{4}$/)) { 
        alert(ic[i] + ": is correct"); 
    } else {
        alert(ic[i] + ": is false");
    }
}

b. Postcode

/^\d{5}$/ is a simple RegExp to detect exactly 5-digit numberic value, e.g:

var postcode = ["83040", "8304", "-5065"];
for(var i = 0; i < postcode.length; i++) {
    if(postcode[i].match(/^\d{5}$/)) { 
        alert(postcode[i] + ": is correct"); 
    } else {
        alert(postcode[i] + ": is false");
    }
}

c. Phone Number

/^(\+6)?\d{2,3}-?\d{6,8}$/ allow us to detect fix-line and also mobile number under Malaysian Telco, e.g:

var phone = ["+6012-3456789", "012-3456789", "034567890", "0123456789"];
for(var i = 0; i < phone.length; i++) {
    if(phone[i].match(/^(\+6)?\d{2,3}-?\d{6,8}$/)) { 
        alert(phone[i] + ": is correct"); 
    } else {
        alert(phone[i] + ": is false");
    }
}

4 Responses to '3 JavaScript Regular Expression for Malaysia Data Format'

Subscribe to comments with RSS or TrackBack to '3 JavaScript Regular Expression for Malaysia Data Format'.
  1. James said, on September 21st, 2008 at 7:45 am

    Hi, I found your blog on this new directory of WordPress Blogs at blackhatbootcamp.com/listofwordpressblogs. I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.

  2. aby said, on January 4th, 2009 at 2:20 am

    hey i wanna ask sumthing…
    how can i trace people by using her i/c num and her full name…
    but i don’t how how to trace her?even her number…

  3. Zaki said, on January 6th, 2009 at 10:54 pm

    Can you elaborate on how to trace her?.. What do you mean by actually tracing here.

  1. 3 JavaScript Regular Expression for Malaysia Data Format…

    Can you share with us the proper Regular Expression (RegExp) for Malaysia’s data format as the following:

    * Identification Card
    * Postcode
    * Phone Number…

Leave a Reply
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">