Forum Moderators: coopster

Message Too Old, No Replies

PHP Validation Code

         

learn4life

10:21 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



I'm wondering if someone can help me. I'm learning PHP so please be gentle. I have created a contact form using html and php. The problem I'm having is with the Email and Phone portion. The user has the option to enter either an email address or a phone number, if ONE of those are entered then the form should be sent.

However, if I enter a phone number and leave the email field blank I get the message: "You have entered an incorrect email address."
If I enter a phone number and an email I get this message: "Please provide an email address or a phone number so that we can contact you."
Also, if an email address is entered I get a message: "You have entered an incorrect email address."

Can someone tell me what I'm doing wrong. I have spent several days changing stuff around, googling to find an answer. but now I'm stumped.

Also, if there is anything that seems out of place, please let me know.

Thanks in advance.

if(!empty($_POST['Email']) && !empty($_POST['Phone'])) {
$status .= "Please provide an email address or a phone number so that we can contact you.<br />";
}
elseif ($email = eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z] {2,3})$", $_POST['Email'])) {
$status .= "You have entered an incorrect email address.<br />";
}
else {
$status .= "You have entered an incorrect email address.<br />";
}
if (empty($_POST['Name'])) {
$status .= "Please enter your name.<br />";
} else {
$name = stripslashes($_POST['Name']);
}
if (empty($message)) {
$status .= "Please enter a message.<br />";
} else {
$message = nl2br(stripslashes($_POST['Message']));
}
if (empty($status)) {
$to = 'myemail@mywebsite.com';
$subject = 'the subject';
$header = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

$message = "
Enquiring About: $department<br />
Name: $name<br />
Email: $email<br />
Phone: $phone<br />
Contact Preference: $contactpreference<br />
Message: $message";

$header = "From: $email\r\n";
$header .= "Reply-To: $emailrn";
$header .= 'X-Mailer: PHP/' . phpversion();

if(mail($to, $subject, $message, $header)) {
$status = "Thank you for your Feedback!<br><br>";
} else {
$status = "There was a problem sending your feedback, please contact the administrator.<br><br>";
}
}
}
?>

g1smd

10:33 pm on Mar 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your regex for valid email addresses will reject many of the newer TLDs such as .info and everything else longer than three characters.

You need to run the individual checks for valid email and for valid telephone number only if they each are non-blank.

You also need to return an error message if both are blank.

learn4life

10:49 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



Your regex for valid email addresses will reject many of the newer TLDs such as .info and everything else longer than three characters.

Can you clarify this a bit more for me? What is regex, tlds, .info. Sorry I'm learning so please bear with me.

coopster

11:15 am on Mar 19, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



regex = regular expressions
tld = Top Level Domain (.com, .net, .org, .info, etc.)

However, first things first. You really need to have a good feel for logic in Control Structures [php.net] as that is fundamental to any type of programming, PHP or otherwise.

if (this is true) { 
// do these statements
} else {
// it returned false, do these statements
}

Also, NEVER trust user-supplied input. You are receiving input via form fields and they are passed back to PHP in the $_POST superglobal [php.net] array. Therefore, check for valid values, as you are doing with your email address via regular expression, and reject anything that doesn't look correct.

learn4life

10:56 pm on Mar 19, 2009 (gmt 0)

10+ Year Member



Day 3 and I haven't made any progress. I'm taking it one step at a time as you've suggested. I've gone through the logic but not sure how to put it together. I have tried so many combinations but as I mentioned I'm teaching myself which is making things a bit difficult as I have no direction except on-line.

The logic in my head is:

If email OR phone is not entered show this message:
"Please provide an email address or a phone number so that we can contact you."

If email is entered check if valid, but then phone is not required.

If phone is entered, email is not required so no need to check if valid.

That's my logic but getting it in code is a struggle. Any help will be appreciated.

----------------------------------------------

if(!empty($_POST['Email']) && !empty($_POST['Phone'])) {
$status .= "Please provide an email address or a phone number so that we can contact you.<br />";
} else {
$email = eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z] {2,3})$", $_POST['Email']);
}

coopster

12:39 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For lengthy validation you can set an array of error messages or at the very least an error indicator.
// Initialize some fields: 
$error = array();
// Check if POST fields are set. If so, trim, otherwise initialize
$email = isset($_POST['Email']) ? trim($_POST['Email']) : '';
$phone = isset($_POST['Phone']) ? trim($_POST['Phone']) : '';
// Do some validation:
if (!$email ¦¦ !isValidInetAddress [webmasterworld.com]($email) {
$error[] = 'You have entered an incorrect email address.<br />';
}
if (!$phone) {
$error[] = 'You have entered an incorrect phone number.<br />';
}
// No errors?
if (!$error) {
// process the successful form submission
}
// Display errors:
$errors = implode('</li><li>', $error);
print "You have errors: <ul><li>$errors</li></ul>";

The forum breaks the pipe symbol so you will need to rekey it if you copy/paste this code. Also note that the function to validate email is a user-defined function that is on another discussion in this forum, follow the link to get that function for your code validation if you so desire.

g1smd

12:49 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the original code you first checked that BOTH were non-blank and gave an error message if both were blank. That is OK.

Immediately before the email-only format check I would have added a line 'IF email is non-blank do this check".

Immediately before the phone-only format check I would have added a line 'IF phone is non-blank do this check".

coopster

1:15 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Oops, that's right, it was one or the other, forgot that part of the logic. Good catch.

And now that I look a bit closer, I also realize you are indeed using an indicator of sorts, the $status variable. Should have grabbed some morning coffee first I guess :P

// Do some validation: 
if (!$email && !$phone) {
$error[] = "Please provide an email address or a phone number so that we can contact you.<br />";
} else {
if ($email && !isValidInetAddress [webmasterworld.com]($email) {
$error[] = 'You have entered an incorrect email address.<br />';
}
if (!$email && !isInvalid($phone)) {
$error[] = 'You have entered an incorrect phone number.<br />';
}
}

I faked a phone validation function there, but you get the idea.

learn4life

9:24 pm on Mar 20, 2009 (gmt 0)

10+ Year Member



I'm not giving up on my original code which is to check if an email OR phone number is entered but I need to put this page on-line so what I'm doing in the meantime is making the phone number a required field as most people have a number, and the email not required.

Although I removed the "empty" part of the code, which I assume is what is checking if the field is empty or not, it's still prompting me to enter an email. I'm not sure why that is happening?

if(!ereg("^([A-Za-z_\.]*)@([A-Za-z_]*)\.([A-Za-z_\.]*)$", $email)) {
$status .= "You have entered an incorrect email address.<br />";
} else {
$name = stripslashes($_POST['Email']);
}

Thanks in advance!

henry0

10:07 pm on Mar 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In case you wondering about the question mark
as per coopster post
in: (for example)
$email = isset($_POST['Email']) ? trim($_POST['Email']) : '';

read about the Ternary operator [php.net]

coopster

12:00 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Thanks for that, henry0, I was going to reference that and forgot.

I'm not giving up on my original code which is to check if an email OR phone number is entered

The user has the option to enter either an email address or a phone number, if ONE of those are entered then the form should be sent.

Please go back and review the code samples offered. You don't have to use the exact same code, but the logic is there to get you started. Replace the second "// Do some validation:" section in the first snippet with the updated snippet after corrections were made that were mentioned/caught by g1smd.