ahazin Posted January 16, 2008 Share Posted January 16, 2008 I have created a form which allows the user to input name, email address and a message this is then sent to a php page which sends the information on to my email address. This is all working fine, but when i tried to add email validation it didnt seem to work for me. I simply wanted to ensure the user had input the @ symbol, nothing more difficult than that. Though if it was easy to implement more/better validation that would be a bonus. this is my code so far: *Note* i have 2 txt boxs called name_field and email_field and a text area called message. <?php $to = "lucealmighty@hotmail.co.uk"; $subject = "Contact Us"; $name_field = $_REQUEST['name_field'] ; $email_field = $_REQUEST['email_field'] ; $message = $_REQUEST['message'] ; $headers = "From: $email_field"; $body = "Name: $name_field \n E-Mail: $email_field \n Message:\n $message"; $sent = mail($to, $subject, $body, $headers) ; if($sent) {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Your message was sent successfully</strong></font>"); } else {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">We encountered an error sending your message. Please try again or call us on 07802 414985</strong></font>"); } ?> When inputting the validation in i input this code: function checkEmail($email_field) { if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email_field)) { return FALSE; } else { return TRUE; } if(checkEmail(email_field) == FALSE) { echo "E-mail entered is not valid."; } else { echo "E-mail entered is valid."; } When this code is input it always says the email address is not valid wether it contains an @ symbol or not. Does anyone know where i could be going wrong. Would really appreciate help on this. Another thing i tried to get working was this, though im not so fussed on getting this section to work though it would be a bonus: if ($name=="" or $message=="" or $email=="") { echo "Please fill up all fields !"; The above also didnt work. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
ahazin Posted January 16, 2008 Author Share Posted January 16, 2008 so my full code is: <?php function checkEmail($email_field) { if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email_field)) { return FALSE; } else { return TRUE; } } $to = "lucealmighty@hotmail.co.uk"; $subject = "Contact Us"; $name_field = $_REQUEST['name_field'] ; $email_field = $_REQUEST['email_field'] ; $message = $_REQUEST['message'] ; if(checkEmail(email_field) == FALSE) { echo "E-mail entered is not valid."; } else { echo "E-mail entered is valid."; } $headers = "From: $email_field"; $body = "Name: $name_field \n E-Mail: $email_field \n Message:\n $message"; $sent = mail($to, $subject, $body, $headers) ; if($sent) {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">Your message was sent successfully</strong></font>"); } else {print ("<br><br><br><br><p align=\"center\"><strong><font face=\"Verdana, Arial, Helvetica, sans-serif\" font color = \"#FFFFFF\">We encountered an error sending your message. Please try again or call us on 07802 414985</strong></font>"); } ?> and for some reason no matter what i input as an email address it always says it is valid. Quote Link to comment Share on other sites More sharing options...
ahazin Posted January 16, 2008 Author Share Posted January 16, 2008 ahh finally after much time and effort i got the email bit working Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.