MySQL Validate Email Records MySQL Validate Email Records mysql mysql

MySQL Validate Email Records


Somethis like this

SELECT * FROM users WHERE email NOT REGEXP '^[^@]+@[^@]+\.[^@]{2,}$';

OR

SELECT * FROM users WHERE email NOT REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$';

Update.It's just an example,how to use regular expression in Mysql.


You can use something like this

SELECT * FROM your_tableWHERE email_col LIKE '%@%.%'

to get (quite) valid emails, excluding the ones that are wrong for sure.
But you should check results to be sure of what you're showing...
Anyway, this is a starting point...


Run this SQL to create trigger inside mysql database, This will prevent inserting wrongly formatted email:

CREATE DEFINER=`root`@`localhost` TRIGGER `users_before_insert` BEFORE INSERT ON `users` FOR EACH ROW BEGINIF (NEW.`email` REGEXP '^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$')=0  THEN        SIGNAL SQLSTATE VALUE '45000'            SET MESSAGE_TEXT = '[table:users] - `email` incorrect format!';    END IF;END