Avatar of peter_coop
peter_coopFlag 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
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Derokorian
Derokorian
Flag of United States of America image

      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

Avatar of peter_coop
peter_coop
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

thank you very much
Avatar of peter_coop
peter_coop
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

thank you
Avatar of kaufmed
kaufmed
Flag of United States of America image

NP. Glad to help  = )
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo