简体   繁体   中英

PHP Check Email Address format Regex

I'm looking to use either PHP or Regex to check an email string to see if it matches a specific format. The format is :

Any number of characters and numbers, but ending in a fullstop then 1 number and @gmail.com

For example :

test2email.7@gmail.com
anothertest.6@gmail.com
323232.3@gmail.com

Your question is vague but if you want to check email is correct or not use this:

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    # valid email
}

http://php.net/manual/en/function.filter-var.php

I'm sure someone is going to mark this as a duplicate, as a quick SO search for " email regex " turns up several results, as does a Google search . Some of your best bets are this thread and perhaps this one , and this article on regular-expressions.info. But, to quote an answer from the first thread:

This question is asked a lot, but I think you should step back and ask yourself why you want to validate email adresses syntactically? What is the benefit really?

  • It will not catch common typos.
  • It does not prevent people from entering invalid or made-up email addresses, or entering someone else's address.

If you want to validate that an email is correct, you have no choice than to send an confirmation email and have the user reply to that. In many cases you will have to send a confirmation mail anyway for security reasons or for ethical reasons (so you cannot eg sign someone up to a service against their will)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM