Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Email Validation -- Facebooks Beginnings

email validation case sensitive

  • Please log in to reply
1 reply to this topic

#1 Jmancuso

Jmancuso

    CC Regular

  • Member
  • PipPipPip
  • 44 posts

Posted 22 February 2012 - 03:01 PM

Jmancuso Tutorials

Most of us programmers have scene The Social Network and know about Facebook. In the humble beginning of facebook, you could only have a Harvard.edu email address in order to join "thefacebook.com". But now why couldnt someone just sign up with any email address? This is what i will show you:

Mark Zuckerberg ("creater of facebook") used Pearl as a language of choice, but in this case, i will show you how to do it in PHP.

The built in ereg() and eregi() functions will help you with this. these functions are called regular expressions. Regular expressions in PHP is pretty much its own language inside of PHP and ill tell you why.



//lets say you have an input feild called email

echo "<input type='text' name='email' /> ";

//now to make it a little easier, lets make the html a little more php friendly

$email = $_POST['email'];

//perfect! now lets make a regular expression. pay attention!

$confirmation = ereg("^.+@.+\..+", $email);

//above is the email confirmation, this ensures that the email address should look pretty similar to an email address.
//now lets make an if statement:

if ($confirmation){
//insert all your database insertion here.
}



that is basic email validation. its not that confusing. ill even tell you what it means in human language.
starting in the ereg() function:

the carat(^) simply means look for - the ".+" means all white space and that @ symbol means @. so so far we have "look for all whitespace, and an @ symbol" now after that @ symbol, we have once again the ".+" signs. which of course means all white space. the "\" symbol means literally. so "\." means litterally look for a period. and then you know what the ".+" means.

so in human terms: look for(^) any whitespace (.+), an @ symbol(@), more whitespace(.+), a period (\.) and then more whitespace(.+)"
in display terms it looks for: whitespace@whitespace.whitespace. now obviously this isnt perfect.

we can make it better:
quick definition: ereg() means regular expressions, eregi() means regular expression non case sensitive, so if you want it to looks for [a-z], it would also look for [A-Z]


//this is what mark zuckerberg used when he was writing code for facebook, he used pearl as a language of chocie but this is the php version:

$email = $_POST['email'];
$confirmation = eregi("^.+@harvard.edu", $email);

//means look for(^) any whitespace(.+) @ harvard.edu. its that simple.

//and when he expanded into other universities:
$confirmation = eregi("^.+@( harvard | standford | columbia ).edu", $email);

//by now you should know what the pipe symbol (|) means, it just means OR. like if (a == a || a==b){}


if you want to validate numbers:


$homephone_confirmation = ereg("^[0-9]{0,1}\-?[0-9]{0,2}\-?[0-9]{0,2}\-?[0-9]{0,3}", $homephone);

//this will check a phone number in this order: 1-482-555-9837 and if they do or dont put hyphens, it doesnt matter.
//if you have a confirmation of a city and state like this: Raleigh North Carolina, 11762 (not sure why you would) but:

$address_confirmation = eregi("^[a-z]\,?[a-z]\,?[0-9]{0,4}", $address)
//this will look for that format, and a 5 digit zipcode at the end (im pretty sure all zip codes are 5 digits)


the brackets just mean hope many numbers are expected.

i hope this helps anyone in the future. now that you have the basic understanding of regular expressions, other tutorials will seem much easier.
  • 1

#2 JasonKnight

JasonKnight

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts
  • Location:Keene, NH
  • Programming Language:C, C++, JavaScript, Delphi/Object Pascal, Pascal, Assembly, Others

Posted 10 April 2013 - 09:44 AM

I always kind of laugh at using regex for valid character checks in php. Why you might ask? FILTER_VAR!!! PHP already HAS a check for valid character sequences - just need to check length and that it's a valid mail domain too.

A more robust mail check a friend and I came up with on another forums:
function isValidEmail($address) {

	/* filter checks for valid chars, but not lengths */
	if (filter_var($address,FILTER_VALIDATE_EMAIL)==FALSE) {
		return false;
	}
	
	/* explode out local and domain for lengths */
	list($local,$domain)=explode('@',$address);
	$localLength=strlen($local);
	$domainLength=strlen($domain);
	
	return (
		/* check for proper lengths */
		($localLength>0 && $localLength<65) &&
		($domainLength>3 && $domainLength<256) &&
		(
			/* and then see if the domains are valid */
			checkdnsrr($domain,'MX') ||
			checkdnsrr($domain,'A')
		)
	);
	
} // isValidEmail
If I were to restrict by domain, I'd have an array of valid domains (in lower case) and check it against strtolower($domain) with in_array.

Also, time to STOP using ereg in PHP was sometime around six years ago, hence the big red warning boxes in the manual?

Edited by JasonKnight, 10 April 2013 - 09:46 AM.

  • 0
The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.





Also tagged with one or more of these keywords: email validation, case sensitive