Skip to content

Instantly share code, notes, and snippets.

@TimoSolo
Created June 26, 2012 11:29
Show Gist options
  • Save TimoSolo/2995270 to your computer and use it in GitHub Desktop.
Save TimoSolo/2995270 to your computer and use it in GitHub Desktop.
South African ID number Validation for openERP
# info from http://geekswithblogs.net/willemf/archive/2005/10/30/58561.aspx
def _id_check(self, cr, uid, ids, context=None):
obj = self.browse(cr, uid, ids[0], context=context)
if obj and obj.idnumber:
idnum = str(obj.idnumber)
if len(idnum) != 13:
return False
checksum1 = 0
checksum2 = ""
checksum3 = 0
for p, ch in enumerate(idnum):
pos = p+1
if pos % 2 and pos != 13:
checksum1 += int(ch)
if not (pos % 2):
checksum2 += ch
checksum2 = int(checksum2) * 2
for p, ch in enumerate(str(checksum2)):
checksum3 += int(ch)
checksum3 += checksum1
if (10 - (checksum3 % 10)) != (int(idnum) % 10): # compare last digits
return False
return True
_constraints = [
(_id_check, 'Invalid SA ID number', ['idnumber'])
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment