Function emailvalidate

Hi Guys,

I am new in PHP and have an issue with emailvalidate function see my code below. This works for any other email validation but except for .africa domain. if I put for example [email protected] it get rejected with an alert “emails is invalid” see my reg expression code below and advise
This @"^[-a-zA-Z0-9][-.a-zA-Z0-9]@[-.a-zA-Z0-9]+(.[-.a-zA-Z0-9]+).
(com|edu|info|gov|int|mil|net|org|biz|name|museum|bank|africa|coop|aero|pro|tv|[a-zA-Z]{2})$"
or
^[a-z][^@]*@(([a-z][a-z0-9-]+).){0,3}(?2)$

Both do not work.
Please assist

Thanks

Peter

The regular expression to validate an email is enormous; I’d recommend you use PHP’s built in functions to do it instead.

filter_var($email, FILTER_VALIDATE_EMAIL);

will return false if $email is not a valid email. Note that this doesn’t check that the email actually exists; the only way to do that is to send a message to the address and ask the recipient to respond.

If you do want to use a regular expression to validate the email, here are the ones used by PHP internally.

1 Like

Thanks for your quick response skawid,

All I need is just to get the email address to be accepted on the input form am not really worried if the email address exist or not.
I know in C# there is something similar to the below which works but as am new on PHP am not sure what is the equivalent of this in PHP?

/^[a-zA-Z\d.]+@[a-zA-Z0-9]+.[-.a-zA-Z0-9]+(com|edu|info|gov|int|mil|net|org|biz|name|museum|bank|africa|coop|aero|pro|tv|[a-zA-Z]{2})$/

If you just need to validate the email, I would use:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // handle invalid email here
}

where $email is the email that has been submitted.

1 Like

As stated, you don’t need the regex, use the function.

Sponsor our Newsletter | Privacy Policy | Terms of Service