Here is a SQL script to show you how it is done:
CREATE SCHEMA IF NOT EXISTS `test` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;USE `test`;CREATE TABLE IF NOT EXISTS `test`.`entity_email` (`emailID` INT NOT NULL,`email` VARCHAR(64) NOT NULL,PRIMARY KEY (`emailID`) )ENGINE = InnoDBCOMMENT = "Generic Email table";DELIMITER $$USE `test`$$CREATE TRIGGER `trg_entity_email_insert` BEFORE INSERT ON `test`.`entity_email` FOR EACH ROWBEGINIF NOT (SELECT NEW.email REGEXP '$[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$') THEN-- bad dataSIGNAL SQLSTATE '40000';END IF;END$$CREATE TRIGGER `trg_entity_email_update` BEFORE UPDATE ON `test`.`entity_email` FOR EACH ROW
BEGINIF NOT (SELECT NEW.email REGEXP '$[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$') THEN-- bad dataSIGNAL SQLSTATE '40000';END IF;END$$
DELIMITER ;
Now MySQL will perform e-mail validation EVERY time you insert or edit the record. If it doesn't match, it will fail (MySQL outputs an "Unhandled user-defined exception condition" as the error message).
Hope this helps!
Thanks a lot! this really help me, There is a tiny mistake in "REGEXP '$["
ReplyDeleteIt should be "REGEXP '^[" with ^ instead of $, this way works like a charm
I appreciated your work very thanks Verifications IO
ReplyDeletehow to get an out put as "wrong email" instead of "Unhandled user-defined exception condition"
ReplyDeleteEmail Validation and Verification, Email Checker and Bulk Verify Tool. Using DeBounce remove invalid, disposable, spam-trap, syntax and deactivated emails. email list cleaning service
ReplyDeleteDeliverBility offers very fast and simple email verification online service that validates your email lists to check for invalid or undeliverable emails so that you can maximize your marketing campaign potential.
ReplyDeleteA good mysql email checker. This script has saved me a lot of stress
ReplyDelete