Oracle determines whether the mobile phone number is legal.
Legal mobile phone numbers: all 11 digits and start with 13, 15, 18.
create or replace function isLegal
(phoneNumber in varchar2) return number
--Returns 0 for legal, returns 1 for illegal
as
begin
if length(phoneNumber) != 11 then return 1;
elsif replace(translate(phoneNumber, '0123456789', '0'), '0', '') IS not NULL THEN
--dbms_output.put_line(phoneNumber ||'is not a numeric sequence of [0-9]');
return 1;
elsif substr(phoneNumber,1,2) not in ('13','15','18') then
--dbms_output.put_line ('First two digits are illegal: '||substr(phoneNumber,1,2));
return 1;
else
--dbms_output.put_line ('legal number');
return 0;
end if;
exception when others then
--dbms_output.put_line ('Exception:');
return 1;
end isLegal;