More protection
 |
- One more thing: numbers only
- Still okay to check elsewhere: levels of logic
|

CREATE OR REPLACE FUNCTION validate_ccnumber(VARCHAR)
RETURNS BOOLEAN AS '
DECLARE
mynumber ALIAS FOR $1;
BEGIN
-- Must be 16 characters long
IF length(mynumber) != 16 THEN
RAISE EXCEPTION \'Credit card numbers must be 16 digits long\';
END IF;
-- Must be number 0-9 only
IF length(substring(mynumber FROM \'[^\\\\d]\')) THEN
RAISE EXCEPTION \'Credit card number must have only digits 0-9\';
END IF;
-- Must have a valid checksum
IF cc_checksum(mynumber) THEN
RAISE EXCEPTION \'Invalid credit card number: checksum failed\n';
END IF;
RETURN TRUE;
END;
' LANGUAGE plpgsql IMMUTABLE;

oscon=> insert into biz values ('123412341234123a');
ERROR: Credit card number must have only digits 0-9

Can we do better?
| |
- Problem with the above?
- Hard-coded errors
- RAISE EXCEPTION \'3752 Invalid credit card number: checksum failed\n';
- C vs. PL/pgsql
- Documentation
|