My Australian Medicare number is 11 numeric digits and includes no letters or other characters.
It is formatted in groups, and the last digit varies according to the member of my family, e.g.:
- Me:
5101 20591 8-1
- My wife:
5101 20591 8-2
- My first child:
5101 20591 8-3
I've seen medicare numbers formatted without the spaces and the dash, but the meaning is the same, so I'd expect to accept 51012059181
as a valid Medicare number as well.
I've also seen context where the last digit is not required or not supposed to be entered; e.g. 5101205918
, I guess where they're only interested in the family as a whole.
Therefore, I think this may be appropriate:
^\d{4}[ ]?\d{5}[ ]?\d{1}[- ]?\d?$
EDIT
Based on the logic in user2247167's answer, I've used the following PL/SQL function in my Apex application to give a user-friendly warning to the user:
FUNCTION validate_medicare_no (i_medicare_no IN VARCHAR2)
RETURN VARCHAR2 IS
v_digit1 CHAR(1);
v_digit2 CHAR(1);
v_digit3 CHAR(1);
v_digit4 CHAR(1);
v_digit5 CHAR(1);
v_digit6 CHAR(1);
v_digit7 CHAR(1);
v_digit8 CHAR(1);
v_check CHAR(1);
v_result NUMBER;
BEGIN
IF NOT REGEXP_LIKE(i_medicare_no, '^\d{10}\d?{2}$') THEN
RETURN 'Must be 10-12 digits, no spaces or other characters';
ELSE
v_digit1 := SUBSTR(i_medicare_no, 1, 1);
IF v_digit1 NOT IN ('2','3','4','5','6') THEN
RETURN 'Not a valid Medicare number - please check and re-enter';
ELSE
v_digit2 := SUBSTR(i_medicare_no, 2, 1);
v_digit3 := SUBSTR(i_medicare_no, 3, 1);
v_digit4 := SUBSTR(i_medicare_no, 4, 1);
v_digit5 := SUBSTR(i_medicare_no, 5, 1);
v_digit6 := SUBSTR(i_medicare_no, 6, 1);
v_digit7 := SUBSTR(i_medicare_no, 7, 1);
v_digit8 := SUBSTR(i_medicare_no, 8, 1);
v_check := SUBSTR(i_medicare_no, 9, 1);
v_result := mod( to_number(v_digit1)
+ (to_number(v_digit2) * 3)
+ (to_number(v_digit3) * 7)
+ (to_number(v_digit4) * 9)
+ to_number(v_digit5)
+ (to_number(v_digit6) * 3)
+ (to_number(v_digit7) * 7)
+ (to_number(v_digit8) * 9)
,10);
IF TO_NUMBER(v_check) != v_result THEN
RETURN 'Not a valid Medicare number - please check and re-enter';
END IF;
END IF;
END IF;
-- no error
RETURN NULL;
END validate_medicare_no;