August 16, 201115 yr <!DOCTYPE HTML> <html> <head> </head> <body> <?php $errors="bad email!" if (isset($_POST['submit'])) { if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { echo("email is cool."); } else { echo('<div style="color: red">' . $errors . '<br/></div>'); } } ?> <form action="insert.php" method="post"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html> Edited August 16, 201115 yr by kingy da killa
August 16, 201115 yr Author 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; } If i put this in my functions.php file, and then add this to to my code that my form calls in, it should work right? also do i have to do an include for functions.php? if (check_email_address($email)) { echo $email . ' is a valid email address.'; } else { echo $email . ' is not a valid email address.'; } Edited August 16, 201115 yr by kingy da killa
August 16, 201115 yr also do i have to do an include for functions.php? Yes. Otherwise the code isn't in the script at all.
August 16, 201115 yr function funcVerifyEmailAddress($vEmailAddress="", $vMX=false) { if (trim($vEmailAddress) == "") { return false; } elseif (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $vEmailAddress)) { return false; } if (($vMX == true) && (!checkdnsrr(substr(strstr($vEmailAddress, '@'), 1),"ANY"))) { return false; } return true; } It should be worth noting that the ereg functions have no been depreciated and/or removed as of php 5.3 so you'll want to move to a preg based method.
August 16, 201115 yr function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if ($valid) { echo $email.' is valid!'; } else { echo $email.' is not valid!'; } } ^^ the best validator ive ever came across, checks for certain characters, checks domain exists, checks theres no dots at start or finish of email an checks some other little bits
August 16, 201115 yr Author Cheers for that snipper thats getting saved for sure Got a clue why this dont work? www.bkwebdesignandhosting.co.uk/php <!DOCTYPE HTML> <html> <head> <?php //* CONNECT DATABASE //* include ('connect.php'); //* CONNECT DATABASE //* //* FORM VARIABLES //* $first = $_POST ['first']; $last = $_POST ['last']; $email = $_POST ['email']; $mobile = $_POST ['mobile']; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); //* FORM VARIABLES //* //* START EMAIL VALIDATION //* function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if ($valid) { echo $email.' is valid!'; } else { echo $email.' is not valid!'; } } //* END EMAIL VALIDATION //* ?> </head> <body> <form action="index.php" method="post"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html>
August 16, 201115 yr Cheers for that snipper thats getting saved for sure Got a clue why this dont work? www.bkwebdesignandhosting.co.uk/php <!DOCTYPE HTML> <html> <head> <?php //* CONNECT DATABASE //* include ('connect.php'); //* CONNECT DATABASE //* //* FORM VARIABLES //* $first = $_POST ['first']; $last = $_POST ['last']; $email = $_POST ['email']; $mobile = $_POST ['mobile']; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); //* FORM VARIABLES //* //* START EMAIL VALIDATION //* function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if ($valid) { echo $email.' is valid!'; } else { echo $email.' is not valid!'; } } //* END EMAIL VALIDATION //* ?> </head> <body> <form action="index.php" method="post"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html> //* FORM VARIABLES //* $first = $_POST ['first']; $last = $_POST ['last']; $email = $_POST ['email']; $mobile = $_POST ['mobile']; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); ^^ that should be in an if statement although thats not the problem i dont think that should be if(isset($_POST['submit'])) { //* FORM VARIABLES //* $first = $_POST ['first']; $last = $_POST ['last']; $email = $_POST ['email']; $mobile = $_POST ['mobile']; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); } that basically checks that the forms being posted EDIT, it is working, it should be submitting something to the database so check its just because your not displaying anything after its submitted Edited August 16, 201115 yr by web-itec
August 16, 201115 yr Author <!DOCTYPE HTML> <html> <head> <?php //* CONNECT DATABASE //* include ('connect.php'); //* CONNECT DATABASE //* //* CHECKS THESE VARIABLES ARE CORRECT BEFORE SENDING //* if(isset($_POST['submit'])) { //* CHECKS THESE VARIABLES ARE CORRECT BEFORE SENDING //* //* FORM VARIABLES //* $first = $_POST ['first']; $last = $_POST ['last']; $email = $_POST ['email']; $mobile = $_POST ['mobile']; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); } //* FORM VARIABLES //* //* START EMAIL VALIDATION //* function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if ($valid) { echo $email.' is valid!'; } else { echo $email.' is not valid!'; } } //* END EMAIL VALIDATION //* ?> </head> <body> <form action="index.php" method="post"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html> so like that?? im logging into php myadmin now to check it it still lets blank entrys go into my database :/ Edited August 16, 201115 yr by kingy da killa
August 16, 201115 yr <!DOCTYPE HTML> <html> <head> <?php //* CONNECT DATABASE //* include ('connect.php'); //* CONNECT DATABASE //* //* CHECKS THESE VARIABLES ARE CORRECT BEFORE SENDING //* if(isset($_POST['submit'])) { //* CHECKS THESE VARIABLES ARE CORRECT BEFORE SENDING //* //* FORM VARIABLES //* $first = $_POST ['first']; $last = $_POST ['last']; $email = $_POST ['email']; $mobile = $_POST ['mobile']; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); } //* FORM VARIABLES //* //* START EMAIL VALIDATION //* function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if ($valid) { echo $email.' is valid!'; } else { echo $email.' is not valid!'; } } //* END EMAIL VALIDATION //* ?> </head> <body> <form action="index.php" method="post"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html> so like that?? im logging into php myadmin now to check it okay cool and ive edited the code for you a little basically this line $email = $_POST ['email']); sets the variable called $email to whatever the function returns the posted email is what is sent to the function ValidateEmail() and then it does all the checks and returns a value, if the emails valid itll return the email, if its not valid itll return false, which then as you can see, the check before it posts it into mysql basically checks the variable if its false, if its false then the emails invalid, if its not false (else) then it will say its valid and then post to the database (but youll remove that it says its a valid email when youve completed the script as users dont need to know that but you can say something like the forms been submitted or something <!DOCTYPE HTML> <html> <head> <?php //* CONNECT DATABASE //* include ('connect.php'); //* CONNECT DATABASE //* //* FORM VARIABLES //* if(isset($_POST['submit'])) { //* FORM VARIABLES //* $first = $_POST ['first']; $last = $_POST ['last']; $email = ValidateEmail($_POST ['email']); $mobile = $_POST ['mobile']; if(!$email) { echo "invalid email"; } else { echo "valid email"; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); } } //* FORM VARIABLES //* //* START EMAIL VALIDATION //* function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if($valid) { return $email; } else { return false; } } //* END EMAIL VALIDATION //* ?> </head> <body> <form action="" method="POST"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html>
August 16, 201115 yr Author <!DOCTYPE HTML> <html> <head> <?php //* CONNECT DATABASE //* include ('connect.php'); //* CONNECT DATABASE //* //* LINE BELOW MAKES SURE SUBMIT BUTTON CHECKS BEFORE SENDING //* if(isset($_POST['submit'])) { //* FORM VARIABLES 2 BE CHECKED //* $first = $_POST ['first']; $last = $_POST ['last']; //* EMAIL VARIABLES 2 BE CHECKED //* $email = ValidateEmail($_POST ['email']); //* MOBILE VARIABLES 2 BE CHECKED //* $mobile = $_POST ['mobile']; $begin = substr($mobile,0,3); $theOthers = 0+substr($mobile,3); $ok = true; echo strlen($mobile); if ($begin != "+44") { $ok = false; } else { if (strlen($mobile)!=13) { $ok = false; } } if ($ok) { echo"valid mobile" } //* MOBILE VARIABLES 2 BE CHECKED //* //* EMAIL ERROR CODE //* if(!$email) { echo "<div style="border 1px #ccc">invalid email</div<"; } else { echo "valid email (You have been added to the database)"; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); } } //* START EMAIL VALIDATION //* function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if($valid) { return $email; } else { return false; } } //* END EMAIL VALIDATION //* ?> </head> <body> <form action="" method="POST"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html> ok theres now added validation for mobile numbers, but acheiving an error any ideas?
August 16, 201115 yr <!DOCTYPE HTML> <html> <head> <?php //* CONNECT DATABASE //* include ('connect.php'); //* CONNECT DATABASE //* //* LINE BELOW MAKES SURE SUBMIT BUTTON CHECKS BEFORE SENDING //* if(isset($_POST['submit'])) { //* FORM VARIABLES 2 BE CHECKED //* $first = $_POST ['first']; $last = $_POST ['last']; //* EMAIL VARIABLES 2 BE CHECKED //* $email = ValidateEmail($_POST ['email']); //* MOBILE VARIABLES 2 BE CHECKED //* $mobile = $_POST ['mobile']; $begin = substr($mobile,0,3); $theOthers = 0+substr($mobile,3); $ok = true; echo strlen($mobile); if ($begin != "+44") { $ok = false; } else { if (strlen($mobile)!=13) { $ok = false; } } if ($ok) { echo"valid mobile" } //* MOBILE VARIABLES 2 BE CHECKED //* //* EMAIL ERROR CODE //* if(!$email) { echo "<div style="border 1px #ccc">invalid email</div<"; } else { echo "valid email (You have been added to the database)"; $query="INSERT INTO member (first, last, email, mobile)VALUES ('".$first."','".$last."','".$email."','".$mobile."')"; mysql_query($query) or die ('Error updating database because '.mysql_error()); } } //* START EMAIL VALIDATION //* function ValidateEmail($email) { $valid = true; $findats = strrpos($email, "@"); if (is_bool($findats) && !$findats) { $valid = false; } else { $domain = substr($email, $findats+1); $local = substr($email, 0, $findats); $locallength = strlen($local); $domainlength = strlen($domain); if ($locallength < 1 || $locallength > 64) { $valid = false; } elseif ($domainlength < 1 || $domainlength > 256) { $valid = false; } elseif ($local[0] == '.' || $local[$locallength-1] == '.') { $valid = false; } elseif ((preg_match('/\\.\\./', $local)) || (preg_match('/\\.\\./', $domain))) { $valid = false; } elseif (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $valid = false; } elseif (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) { if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) { $valid = false; } } elseif (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $valid = false; } } if($valid) { return $email; } else { return false; } } //* END EMAIL VALIDATION //* ?> </head> <body> <form action="" method="POST"> First Name: <input type="text" name="first" id="first"></br> Last Name: <input type="text" name="last"></br> Email: <input type="text" name="email"></br> Phone: <input type="text" name="mobile"></br> <input type="submit" name="submit"> </form> </body> </html> ok theres now added validation for mobile numbers, but acheiving an error any ideas? you didnt put a ; on the end of echo"valid mobile" also i wouldnt use that mobile check, as it appears to check for +44, if i was to send you my mobile number i would start it with 0 not +44 Edited August 16, 201115 yr by web-itec
Create an account or sign in to comment