Regular expression for validating email address
Among the permitted characters are some that present a security risk if passed directly from user input to an SQL statement, such as the single quote (') and the pipe character (|).
Be sure to escape sensitive characters when inserting the email address into a string passed to another program, in order to prevent security holes such as SQL injection attacks: Both the local part and the domain name can contain one or more dots, but no two dots can appear right next to each other.
Finally, you have to consider how future-proof you want your regular expression to be.
In the past, it made sense to restrict the top-level domain to only two-letter combinations for the country codes, and exhaustively list the generic top-level domains—that is, The regular expressions presented in this recipe show all the basic parts of the regular expression syntax in action.
You want to use a regular expression to validate this email address before trying to send email to it.
This reduces the number of emails returned to you as undeliverable.
There is no top-level domain is silently discarding mail sent to nonexistent mailboxes, or if John Doe hit the Delete button on his keyboard, or if his spam filter beat him to it.
All the regular expressions, except the “simple” one, require the case-insensitive matching option to be turned on.On the final line we call test method for our regular expression and pass the email address as input.If the input email address satisfies our regular expression, ‘test’ will return true otherwise it will return false. You can call this method whenever you want to validate email address.Though it obviously allows many things that aren’t email addresses, such as , the regex is quick and simple, and will never block a valid email address.If you want to avoid sending too many undeliverable emails, while still not blocking any real email addresses, the regex in Top-level domain has two to six letters is a good choice.
Search for regular expression for validating email address:
But if you’re scanning database files that you know contain only valid email addresses, you can use a very simple regex that merely separates the email addresses from the other data.