Correct Phone Number not passing validation. I think...

the mail() function

Moderators: egami, macek, gesf

Post Reply
G33kman84
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 29, 2015 1:18 pm

Wed Jun 15, 2016 7:12 am

So I'm having an issue where my form says that the phone number has not been entered correctly when it in fact has.

Here's my PHP Code:

Code: Select all

//Phone Number
	if (empty($_POST["phone"])) {
		$phoneERR = "A phone number is required";
		} else {
			if(preg_match('/^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/', $phone)) {
				$phone = preg_replace('/[^0-9]/', '', $phone);
				return $phone;
			} else {
				$phoneERR = "Please enter a valid phone number";
			}
		}
Here's the Form Section

Code: Select all

<div>
																	<label for="phone"><strong>Phone Number:</strong><i class="fa icon fa-asterisk asterisk" id="asterisk"></i> (Ex. 555-555-5555)</label>
																	<span class="error"><?php echo $phoneERR ?></span>
																	<input type="text" name="phone" value="<?php echo $phone ?>" />
																</div>
So whenever I enter a phone number whether it is correct or not I get the "Please enter a valid phone number" from the phoneERR section...
Any help?

element121
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Sat Jun 06, 2015 11:46 am
Contact:

Wed Jun 15, 2016 10:15 am

Is your $phone variable assigned a value, it's not in this snippet:

Code: Select all

//Phone Number
   if (empty($_POST["phone"])) {
      $phoneERR = "A phone number is required";
      } else {
         if(preg_match('/^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/', $phone)) {
            $phone = preg_replace('/[^0-9]/', '', $phone);
            return $phone;
         } else {
            $phoneERR = "Please enter a valid phone number";
         }
      }
You would need to assign the POSTed value to the variable before the preg line.

Code: Select all

$phone = $_POST["phone"];
You should also filter POSTed values

Code: Select all

$phone = filter_var ( $_POST["phone"], FILTER_SANITIZE_STRING);

G33kman84
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 29, 2015 1:18 pm

Thu Jun 16, 2016 7:08 am

Perfect I will give that a try thanks!

Post Reply