Well, checks you are performing are rather week. a@b
contains the @
sign, but that's not a valid e-mail address. Or, string whose length is lower than 10 characters may be 123AB-$)1
but that's not a valid phone number.
I'm not saying that functions I'm going to post do it 100% right, but should be a lot better than what you're trying to do. Adjust them, if you want.
Phone numbers:
CREATE OR REPLACE
FUNCTION f_phone (par_telefon IN VARCHAR2)
RETURN VARCHAR2
IS
/* 21.11.2022 Check whether phone number(s) passed via PAR_TELEFON are
valid.
Check:
- length has to be between 6 (e.g. 654123) and 20 (e.g. +385 91/123-4567) characters
- can contain signs: +-/, space, digits
Return:
- phone number and its errors (if there are any)
- NULL if everything is OK
*/
retval VARCHAR2 (200);
l_str VARCHAR2 (200); -- list of errors
l_cnt NUMBER := 0; -- number of errors
BEGIN
FOR cur_r IN ( SELECT REGEXP_SUBSTR (par_telefon,
'[^,]+',
1,
LEVEL) val
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT (par_telefon, ',') + 1)
LOOP
IF LENGTH (cur_r.val) < 6
THEN
l_str := l_str || '; ' || 'too short';
l_cnt := l_cnt + 1;
ELSIF LENGTH (cur_r.val) > 20
THEN
l_str := l_str || '; ' || 'too long';
l_cnt := l_cnt + 1;
END IF;
IF TRANSLATE (cur_r.val, '~1234567890-/+ ', '~') IS NOT NULL
THEN
l_str := l_str || '; ' || 'allowed: 0-9 +-/';
l_cnt := l_cnt + 1;
END IF;
IF l_cnt > 0
THEN
retval :=
retval || '; ' || cur_r.val || ': ' || LTRIM (l_str, '; ');
l_cnt := 0;
l_str := NULL;
END IF;
END LOOP;
RETURN ltrim(retval, '; ');
END f_phone;
/
E-mail addresses:
CREATE OR REPLACE
FUNCTION f_e_mail (par_e_mail IN VARCHAR2)
RETURN VARCHAR2
IS
/* 21.11.2022 Check e-mail addresses.
*/
l_email VARCHAR2 (200);
retval VARCHAR2 (200);
l_str VARCHAR2 (200); -- list of errors
l_cnt NUMBER := 0; -- number of errors
BEGIN
l_email := RTRIM (par_e_mail, CHR (13));
FOR cur_r IN ( SELECT TRIM (REGEXP_SUBSTR (REPLACE (l_email, ' ', ''),
'[^,]+',
1,
LEVEL)) val
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT (l_email, ',') + 1)
LOOP
IF NOT REGEXP_LIKE (
cur_r.val,
'^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}$')
THEN
l_str := l_str || '; ' || 'invalid e-mail address';
l_cnt := l_cnt + 1;
END IF;
IF l_cnt > 0
THEN
retval :=
retval || '; ' || cur_r.val || ': ' || LTRIM (l_str, '; ');
l_cnt := 0;
l_str := NULL;
END IF;
END LOOP;
RETURN ltrim(retval, '; ');
END f_e_mail;
/
A test or two:
SQL> select f_phone('555 123-456') phone_1,
2 f_phone('00385-AB-234 234') phone_2,
3 f_phone('123') phone_3,
4 f_phone('456ab, +385 81 123-456') phone_4
5 from dual;
PHONE_1 PHONE_2 PHONE_3 PHONE_4
---------- ---------------------------------------- --------------- ----------------------------------------
00385-AB-234 234: allowed: 0-9 +-/ 123: too short 456ab: too short; allowed: 0-9 +-/
SQL>
SQL> select f_e_mail('a@b') mail_1,
2 f_e_mail('little@stack.com, foot@yahoo') mail_2
3 from dual;
MAIL_1 MAIL_2
---------------------------------------- ----------------------------------------
a@b: invalid e-mail address foot@yahoo: invalid e-mail address
SQL>