Avatar of peter_coop
peter_coop
Flag for United Kingdom of Great Britain and Northern Ireland asked on

email regex function not working

I am trying to validate email address's in a form and cannot get the code to work. It prints the messages but when a valid email address is included, it still prints the message ' echo 'You must enter a valid email address format'; '. I am using the elseif after an if statement and have only posted the relevant part of the code. Can spmeone tell me where I am going wrong? Many thanks
elseif ($email == '') {
        echo 'You must select enter an email address';
        }
      elseif ($email !=eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($email))) {
        echo 'You must enter a valid email address format';
        }

Open in new window

PHPWeb Development

Avatar of undefined
Last Comment
kaufmed

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
kaufmed

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Derokorian

      elseif ($email !=eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($email))) {

Open in new window


Checks if $email is equal to the return of the function which would be: Returns the length of the matched string if a match for pattern was found in string, or FALSE if no matches were found or an error occurred. So you probably just want the function in there. Also you can use filter_var for to make this easier:

elseif( filter_var($email,FILTER_VALIDATE_EMAIL) ) {

Open in new window

peter_coop

ASKER
thank you very much
peter_coop

ASKER
thank you
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
kaufmed

NP. Glad to help  = )