How to validate I/C Number?

1,186 views
Skip to first unread message

Fadhli

unread,
Jun 23, 2008, 8:27:22 PM6/23/08
to Malaysia Ruby Brigade
Hello,

Has anyone develop an application that he needs to validate a I/C
Number?

Did you do a regex pattern match? Do you mind sharing it?

Thanks

kamal

unread,
Jun 23, 2008, 10:05:41 PM6/23/08
to Malaysia Ruby Brigade
I've written I/C validation before, but only for new I/C numbers. It's
a combination of regexp, parsing the first six digits as a date, and
checking the 7-8th characters against known states from
http://en.wikipedia.org/wiki/MyKad

Unfortunately I don't have the code with me anymore (freelance project
that has been handed off). Perhaps Aizat should release

validates_mykad :ic ?


Regards,
kamal

Fadhli

unread,
Jun 24, 2008, 2:32:00 AM6/24/08
to Malaysia Ruby Brigade
Bummer.

I'm currently just doing a very simplistic regex pattern assuming that
the user key in without the dashes (e.g 541031105785)

validates_format_of :icno, :with => /\d{12}\z/ ,:message => 'some msg
here'

Didn't occur to me to parse the dates and states but really cool idea.

Sure would be nice to know any other solutions the malaysian
brigadiers comes up with.

Thanks

Fadhli

On Jun 24, 12:05 pm, kamal <kamal.fa...@gmail.com> wrote:
> I've written I/C validation before, but only for new I/C numbers. It's
> a combination of regexp, parsing the first six digits as a date, and
> checking the 7-8th characters against known states fromhttp://en.wikipedia.org/wiki/MyKad

Pengzhi

unread,
Jun 27, 2008, 9:00:51 PM6/27/08
to Malaysia Ruby Brigade
You can write custom validation:

Class Product < ActiveRecord::Base
validate :verify_mykad

def verify_mykad
self.errors.add("Invalid MyKad No.") unless /your fancy
regex/ =~ self.mykad
end

#also, you can get rid of space in mykad by doing this
def mykad=(string)
write_attribute(:mykad, string.gsub(/(\W)/), '')
end
end

of course, you probably want a more complex regex for validation

fadhli

unread,
Jun 27, 2008, 11:20:49 PM6/27/08
to malay...@googlegroups.com
Thanks Pengzhi, that was very helpful.
--
Woody Allen  - "I am not afraid of death, I just don't want to be there when it happens."
Reply all
Reply to author
Forward
0 new messages