Once only regexp suggestted doesn't consider all errors, I complete on my own function. See the comments into code.
DELIMITER $$
CREATE OR REPLACE function fn_DOC_Validar_EMail ( pStrEMail VARCHAR(200) )
RETURNS BIT
BEGIN
declare lIntValid bit;
set lIntValid = 0;
SELECT 1 into lIntValid
Where pStrEMail REGEXP '^[a-zA-Z0-9][a-zA-Z0-9.!#$%&\'*+-/=?^_`{|}~]*?[a-zA-Z0-9._-]?@[a-zA-Z0-9][a-zA-Z0-9._-]*?[a-zA-Z0-9]?\\.[a-zA-Z]{2,63}$'
and pStrEMail not like '%[^a-z0-9@._-]%' -- not allow characters differents: a-z 0-9 @ . _ -
and pStrEMail not like '%@%@%' -- not allow two @
and pStrEMail not like '%.@%' -- not allow .@
and pStrEMail not like '%..%' -- not allow ..
and pStrEMail not like '%.' -- not allow . (dot) at end
and pStrEMail like '%_@_%_.__%' -- not allow short, i.e., [email protected]
and pStrEMail not LIKE '%^%' -- not allow character ^
and pStrEMail not LIKE '%\%%' -- not allow character %
;
return lIntValid ;
END;
$$
Use the samples bellow to Test your function.
Select Sequencial, email, fn_DOC_Validar_EMail(EMail) from TEMP_Emails;
Create table TEMP_Emails
(
Sequencial int,
Email varchar(200)
);
-- invalids
insert into TEMP_Emails values (1, '@teste.com'); -- Start with @
insert into TEMP_Emails values (2, 'josue@teste'); -- with out domain
insert into TEMP_Emails values (3, 'jo ue@teste'); -- espace
insert into TEMP_Emails values (4, 'jo"ue@teste'); -- quotes
insert into TEMP_Emails values (5, 'jo$ue@teste'); -- special Character
insert into TEMP_Emails values (6, 'josue^[email protected]'); -- special Character
insert into TEMP_Emails values (7, 'josue][email protected]'); -- special Character
insert into TEMP_Emails values (8, 'josue%[email protected]'); -- special Character
insert into TEMP_Emails values (9, '[email protected]'); -- @.
insert into TEMP_Emails values (10, 'josue@@teste.com'); -- 2 x @
insert into TEMP_Emails values (11, 'josue@[email protected]'); -- 2 x @
insert into TEMP_Emails values (12, '[email protected]'); -- .@
insert into TEMP_Emails values (13, '[email protected]'); -- ..
insert into TEMP_Emails values (14, '[email protected].'); -- . at final
--OK: Valids
insert into TEMP_Emails values (101, '[email protected]');
insert into TEMP_Emails values (102, '[email protected]');
insert into TEMP_Emails values (103, '[email protected]');
insert into TEMP_Emails values (104, '[email protected]');
insert into TEMP_Emails values (105, '[email protected]');
insert into TEMP_Emails values (106, '[email protected]');
insert into TEMP_Emails values (107, '[email protected]');
insert into TEMP_Emails values (108, '[email protected]');
insert into TEMP_Emails values (109, '[email protected]');
insert into TEMP_Emails values (110, '[email protected]');
Select Sequencial, email, fn_DOC_Validar_EMail(EMail) from TEMP_Emails;