Jump to content

Recommended Posts

Hi,

I'm using a function I took from the web for email validation, but there's something wrong with the form in general when I use this.

When I test it out, I get the messages "Please enter a valid email address.Thank you..." when I input the letter 'o' into all input fields.

 

I don't know why the validation isn't working for the "Thank you" message, but I think it has something to do with this email.

 

Could anyone help me out here?

 

function:

function check_email_address($email) { 

// First, we check that there's one @ symbol, and that the lengths are right 
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { 

// Email invalid because wrong number of characters in one section, or wrong number of @ symbols. 
return false; 

}

// Split it into sections to make life easier 
$email_array = explode("@", $email); 
$local_array = explode(".", $email_array[0]); 

for ($i = 0; $i < sizeof($local_array); $i++) { 
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { 
	return false; 
	} 
} 
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { 

	// Check if domain is IP. If not, it should be valid domain name 
	$domain_array = explode(".", $email_array[1]); 
	if (sizeof($domain_array) < 2) { 
		return false; 

		// Not enough parts to domain 
		} 

	for ($i = 0; $i < sizeof($domain_array); $i++) { 
		if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { 
			return false; 
		} 
	}
}

return true; 
}

 

From the tutorial, this is how you're supposed to use the function:

if (check_email_address($email)) { 
echo $email . ' is a valid email address.'; 
} else { 
echo $email . ' is not a valid email address.'; 
}

 

The form:

include_once('php/functions.php');

$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$email = $_POST['email'];
$reason = $_POST['reason'];
$i = 0;

if(isset($_POST['submit'])){

if (empty($firstname)){
echo '<span class="error">Please enter your First name.</span>';
}

if (empty($lastname)){
echo '<span class="error">Please enter your Last name.</span>';
}

if (check_email_address($email)){
	$i=1;
}	
else {
	echo '<span class="error">Please enter a valid email address.</span>';
	$i=0;
}

if (empty($reason)){
echo '<span class="error">Please enter your reason to contact me.</span>';
}

if (empty($firstname) && empty($lastname) && ($i=0) && empty($reason)){
echo '<span class="error">All fields are required.</span>';
}

if (!empty($firstname) && !empty($lastname) && ($i=1) && !empty($reason)){
echo '<span class="correct">Thank you...</span>';
}	

}

?>

<div id="form">
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	<table>
		<tr><td>First name:</td><td><input type="text" name="first_name" value="<?php echo $firstname; ?>"/></td></tr>
		<tr><td></td><td><?php echo $firstname_error; ?></td></tr>
		<tr><td>Last name: </td><td><input type="text" name="last_name" value="<?php echo $lastname; ?>"/></td></tr>
		<tr><td></td><td><?php echo $lastname_error; ?></td></tr>
		<tr><td>Email Address: </td><td><input type="text" name="email" value="<?php echo $email; ?>"/></td></tr>
		<tr><td></td><td><?php echo $email_error; ?></td></tr>
		<tr><td>Reason: </td><td><textarea rows="5" cols="16" name="reason" ><?php echo $reason; ?></textarea></td></tr>
		<tr><td></td><td><?php echo $reason_error; ?></td></tr>
		<tr><td> </td><td><input type="submit" name="submit" id="submit" value="submit" /></td></tr>
	</table>
</form>
</div>

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/238900-email-validation/
Share on other sites

His issue is the script continues to e-mail even though an invalid address was entered.

just reread the op, didnt realize he was receiving both messages, if $i is still being set to 1 even though it is not a valid email, something must be wrong in the ereg() function used

Link to comment
https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227595
Share on other sites

I've used other email validation functions and I get the same problem.

 

Does this mean there must be something wrong with the validation part:

	if (!empty($firstname) && !empty($lastname) && ($i=1) && !empty($reason)){
echo '<span class="correct">Thank you...</span>';
}

Link to comment
https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227623
Share on other sites

Problem solved (still using original function).

 

The flag variables $i weren't working for some reason.

 

New code statement:

	if (!empty($firstname) && !empty($lastname) && (check_email_address($email)) && !empty($reason)){
echo '<span class="correct">Thank you...</span>';
}

 

Link to comment
https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227626
Share on other sites

This logic is more sound

 

	if (empty($firstname))
	echo '<span class="error">Please enter your First name.</span>';
elseif (empty($lastname))
	echo '<span class="error">Please enter your Last name.</span>';
elseif (!check_email_address($email)){
	echo '<span class="error">Please enter a valid email address.</span>';
	$i=0;
} elseif (empty($reason))
	echo '<span class="error">Please enter your reason to contact me.</span>';
else
	echo '<span class="correct">Thank you...</span>';

Link to comment
https://forums.phpfreaks.com/topic/238900-email-validation/#findComment-1227727
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.