Search This Blog

2023/04/18

MySQL:Validate Email

MySQL does not provide a built-in function to validate email addresses.
However, you can use a regular expression to validate email addresses in MySQL.


SELECT email FROM employees WHERE
email REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$';

SELECT email,email REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
FROM employees;

Output:
+-----------------------+-----------------------------------------------------------------+
| email | email REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' |
+-----------------------+-----------------------------------------------------------------+
| sangram2681#gmail.com | 0 |
| sagar1778@gmail.com | 1 |
+-----------------------+-----------------------------------------------------------------+

The regular expression pattern
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ matches email
addresses with the following criteria:

Begins with one or more letters, numbers, periods, underscores, percent
signs, plus signs,
or hyphens
Followed by an "@" symbol
Followed by one or more letters, numbers, periods, or hyphens
Followed by a period
Followed by two or more letters

No comments:

Post a Comment