Jump to content

VALIDATE E-MAIL IN EXISTING PHP SCRIPT - 03.19.12


mrjap1

Recommended Posts

Hello Everyone,

 

I've successfully created a form script to check to make sure all my fields are filled out.

 

However, I wanted to create an if statement to check and see if the email address is valid and it contains both and "@", "." symbols. If not then echo '' This is not a Valid email address " if  its not correct.

 

Could someone tell me the proper way to write this and incorporate this into my existing script below.

 

 

Thank you all Again for your Help

 

 

thx

mrjap1

 

 


// Confirm All feild were filled out when submit button was pressed
if($name && $email && $username && $password && $confirm_password) 
{
// Confirm that the NAME that you used is NOT greater than 30 characters	 
	 if(strlen($name)>24)
	 {
	 echo "<h2><center>YOUR NAME IS TOO LONG!!!!</center></h2><br>";
	 }
// Confirm that the EMAIL that you used is NOT greater than 25 characters	 
	 if(strlen($email)>25)
	 {
	 echo "<h2><center>YOUR EMAIL IS TOO LONG!!!!</center></h2><br>";
	 }	 

// Confirm that the USERNAME that you used is NOT greater than 10 characters	 	
	if(strlen($username)>10)
	 {
	 echo "<h2><center>YOUR USERNAME IS TOO LONG!!!!</center></h2><br>";
	 }
	 else {

// Confirm that the PASSWORD that you used MATCH & Between 6 and 15 characters	 
		if(strlen($password)>10 || strlen($password)<6)
		 {
		 echo "<h2><center>YOUR PASSWORD MUST BE BETWEEN 6 and 15 CHARACTERS!!!!</center></h2><br>";
		 }
		if($password == $confirm_password)
		{

Link to comment
Share on other sites

Verifying that a string is a valid email address is not a simple matter. Email addresses can be very convoluted. If you search for it in Yahoo, Google, or any other search engine you will find many, many entries.

 

If you just want to check for the "@" and the "." you can use strpos and strrpos.

 

$posAt = strpos($email, "@");
$posDot = strrpos($email, ".");
if ( ($posAt === false) or ($posDot === false) or ($posDot < $posAt) ) {
   // Bad Address

 

I used strrpos() because there can be full-stops before the "@" as well as after. You may want to improve these checks since there must be at least one character before the "@" and at least one between the "@" and the "." and the "." cannot be at the beginning or the end. ... as I said, this testing gets very complicated.

 

Note: My business email address is 26 characters. So I guess you don't want me at your website. The maximum length of an email address is not clearly defined, although there are limits on the various components of the address. I usually set my database field to VARCHAR(255), which is still less than the "legal" limit.

 

Note: The full-stop (".") is not required. There are valid email addresses at the TLD's (i.e. someone@com); although you are not likely to see many of these at your site.

Link to comment
Share on other sites

Hello DavidAM,

 

I appreciate you taking your time out to reply to my php question. With that said, please look at my code posted below regarding the overall validation of my e-mail address field.

 

Please let me know if this is indeed the proper way to have integrated your suggestion.

Also please state exactly how it should be coded to yield the result that I desire.

 

I appreciate your time...

 

 

Thx Again,

mrjap1

 


if($_POST['submit'])
{ //Begining of full IF Statment
$name = $_POST['name'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
$registration_date = $_POST['registration_date'];

$posAt = strpos($email, "@");
$posDot = strrpos($email, ".");


// Confirm that the EMAIL that you used is NOT greater than 30 characters	

	 if(strlen($email)>30)
	 {
	 echo "<h2><center>YOUR EMAIL IS TOO LONG!!!!</center></h2><br>";
	 }

	if ( ($posAt === false) or ($posDot === false) or ($posDot < $posAt) ) 
	{
	   echo "<h2><center>BAD EMAIL ADDRESS!!!! PLEASE TRY AGAIN!!!</center></h2><br>";
	}	 

Link to comment
Share on other sites

It looks like you got it. If you put those two IF statements in place of the one in your first post, it should work fine.

 

As long as you keep in mind that this does not mean that the email address is "valid" (whatever that means). Even if you write THE perfect test script to check the email address (the one I currently use is probably around 100 lines of code) you can't tell if the email address is a working email address without contacting the SMTP server (for that address) and asking if it can deliver to it. This is one of the reasons that most sites require you to respond to an email in order to complete the registration. Your response to the email is the closest proof they can get that the address is valid and it belongs to the person submitting the form.

Link to comment
Share on other sites

filter_var with the FILTER_VALIDATE_EMAIL flag, and this:

 

This is one of the reasons that most sites require you to respond to an email in order to complete the registration. Your response to the email is the closest proof they can get that the address is valid and it belongs to the person submitting the form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.