NHS Number Validation (Modulus 11)

309 views
Skip to first unread message

Ben Sharif

unread,
Feb 11, 2016, 3:30:35 PM2/11/16
to commcare-users
Here's a snippet of code that I would have found useful about 30 minutes ago. 

I needed to allow users to enter a United Kingdom NHS Number. 
This is a 10 digit number (9 real, 1 check digit). It is validated using the Modulus 11 algorithm

Here's the expression syntax to validate it (assuming your Question ID is 'NHS_Number'):
11 - (substr(/data/NHS_Number, 0, 1) * 10 + substr(/data/NHS_Number, 1, 2) * 9 + substr(/data/NHS_Number, 2, 3) * 8 + substr(/data/NHS_Number, 3, 4) * 7 + substr(/data/NHS_Number, 4, 5) * 6 + substr(/data/NHS_Number, 5, 6) * 5 + substr(/data/NHS_Number, 6, 7) * 4 + substr(/data/NHS_Number, 7, 8) * 3 + substr(/data/NHS_Number, 8, 9) * 2) mod 11 = substr(/data/NHS_Number, 9, 10)

Hope it helps someone,

Ben

Eric Stephan

unread,
Feb 12, 2016, 12:15:53 AM2/12/16
to commcare-users
great that you are sharing your code!

nothing like a nice big tangle of string processing functions... when it all works out you feel you have really created a great beast.

Eric Stephan

unread,
Feb 12, 2016, 12:18:14 AM2/12/16
to commcare-users
Actually, quite similar -- if anyone ever wants to talk about the methods you need to take to assemble good UIC codes from a handful of questions asked of a given client... like "what province were you born in... what's the first letter of your first name..." ... I'm happy to share our experiences too. When you are dealing with non-latin alphabets like Lao and Thai, and using lookup tables to allow you to pick alphabet letters, it gets to be pretty fancy after a while.

Ameera Hamid

unread,
Feb 15, 2016, 5:35:10 AM2/15/16
to commcare-users
Hi Eric,

Is there a way to validate South African ID numbers in a similar way?  Or would I need a database to verify this against?

Kindly,
Ameera

Ben Sharif

unread,
Mar 2, 2016, 1:31:23 PM3/2/16
to commcare-users
It appears that South Africa use the Modulus 10/Luhn Algorithm to validate.
While my code above won't help you I'm sure you could re-write it to fit the Luhn algorithm rules.
Reply all
Reply to author
Forward
0 new messages