GrizzlyBear Posted July 9, 2008 Share Posted July 9, 2008 Hi I need a bit of help with my phone number validation. The number is a 10 digit number eg: 0722106034. The code I am using is just not working. Also the email validation works, but displays the message in a new html page. Is there anyway of changing the code to display it in the same page as the form? Thank You Here is the code: <?php $to = '[email protected]'; $email = $_POST['email']; $name = $_POST['name']; $cellphone = $_POST['cellphone']; $company = $_POST['company']; $position = $_POST['position']; $enquiry = $_POST['enquiry-type']; 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; } function check_phone_number($cellphone) { $cellphone = preg_replace('/[^0-9]/', '', $cellphone); if (preg_match('/^1?[0-9]{10}$/', $cellphone)) { return false; } return true; } if (check_email_address($email) && check_phone_number($cellphone)) { $message = <<<MESSAGE <html> <head> <title>Contact Form</title> </head> <body> <table style="font:Arial, Helvetica, sans-serif" style="font-size:12px" border="0" cellspacing="3" align="center"> <tr> <td align="center" bgcolor="#999999" colspan="2"><strong>CONTACT FORM</strong></td> </tr> <tr> <td bgcolor="#FFFFFF" colspan="2"></td> </tr> <tr> <td bgcolor="#999999" colspan="2"><strong>$enquiry</strong></td> </tr> <tr> <td bgcolor="#999999" width="150">Name</td> <td bgcolor="#cccccc" width="300">$name</td> </tr> <tr> <td bgcolor="#999999" width="150">Cell Number</td> <td bgcolor="#cccccc" width="300">$cellphone</td> </tr> <tr> <td bgcolor="#999999" width="150">Company</td> <td bgcolor="#cccccc" width="300">$company</td> </tr> <tr> <td bgcolor="#999999" width="150">Position</td> <td bgcolor="#cccccc" width="300">$position</td> </tr> </table> </body> </html> MESSAGE; $subject = 'CONTACT FORM'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: ' . $email; mail($to, $subject, $message, $headers); header('Location: http://www.yellowwoodestates.co.za/confirmed.html'); }else { echo("<HTML><BODY>Sorry, this does not appear to be a valid email address! Please go back and try again."); echo("</BODY></HTML>"); } ?> (edited by kenrbnsn to add tags) Link to comment https://forums.phpfreaks.com/topic/113913-phone-number-validation/ Share on other sites More sharing options...
revraz Posted July 9, 2008 Share Posted July 9, 2008 The Regex forum has good examples for validations like this. Link to comment https://forums.phpfreaks.com/topic/113913-phone-number-validation/#findComment-585346 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.