logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Email validation in php
Author
rajendran



Joined: 04 Mar 2013
Posts: 1
Location: Brampton, Canada

PostPosted: Mon 04 Mar '13 19:41    Post subject: Email validation in php Reply with quote

This is the code for validating your email address in php scripting language.

function checkEmail($email){
if(preg_match("/[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) > 0)
{
return true;
}
else
{
return false;
}
}




rajendran
Back to top
sduchet



Joined: 30 Dec 2024
Posts: 1

PostPosted: Mon 30 Dec '24 15:45    Post subject: I tried Reply with quote

Sorry I tried your function but didn't work
please explain where to write this code
Back to top
Otomatic



Joined: 01 Sep 2011
Posts: 223
Location: Paris, France, EU

PostPosted: Mon 30 Dec '24 17:46    Post subject: Reply with quote

Hi,

Sorry, but it's a little more complicated!

Already your regex gives an error:
Code:
PHP Warning:  preg_match(): Compilation failed: range out of order in character class at offset 12

The correction is :
Code:
preg_match("/[a-zA-Z0-9_\-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email)

Emails like '.otomatic@otomatic.net' or 'otomatic.@otomatic.net' or 'oto..matic@otomatic.net' are seen as valid, whereas a . point can't be at the beginning or end of an element, and there can't be consecutive points ..

There are plenty of other errors, such as minimum and maximum element lengths, etc.

In short, a thorough overhaul, I'm sorry to say!
Back to top
timo



Joined: 03 Jun 2012
Posts: 46
Location: FI, EU

PostPosted: Tue 31 Dec '24 15:13    Post subject: Reply with quote

Why not use PHP's filter_var() function?

filter_var($email, FILTER_VALIDATE_EMAIL)

Note that return values would be different: false on fail and $email on success
Back to top
Stray78



Joined: 15 Apr 2024
Posts: 29
Location: USA

PostPosted: Wed 01 Jan '25 6:21    Post subject: Reply with quote

timo wrote:
Why not use PHP's filter_var() function?

filter_var($email, FILTER_VALIDATE_EMAIL)

Note that return values would be different: false on fail and $email on success


That's what I do. Smile
Code:
   // Check if email has been entered and is valid
   if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
      $errEmail = 'Please enter a valid email address!';
   }
Back to top


Reply to topic   Topic: Email validation in php View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner