0

Please, I would like to have a function that checks the email (I copied the regex from this website, I just scaped the ' signs and didn't change anything else) in the most robust way. It's not only a constraint, I need to check the parameter for search functions too.

The idea is that this function should return TRUE if the email provided is valid (RFC 5322) and FALSE if not.

CREATE FUNCTION _is_email_valid(p_email TEXT)
RETURNS BOOLEAN AS $$
BEGIN 
    RETURN REGEXP_MATCHES(p_email, '^(?:[a-z0-9!#$%&''*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])^','g');
END
$$  LANGUAGE plpgsql;

Any help would be much appreciated.

Thanks

jfbaro
  • 301
  • 2
  • 10

0 Answers0