0

Okai, so I was running a check on my website and asked a couple of people if they could register on my page and some of the people managed to get through my registration without setting date (it turned automatically to 00-00-0000). This is the assigning of the variables (using 1 common submit button).

$username = $_POST['username'];
        $passwordone = $_POST['passwordone'];
        $passwordtwo = $_POST['passwordtwo'];
        $name = $_POST['fullname'];
        $email = $_POST['email'];
        $age = $_POST['date'];
        $country = $_POST['country'];

the statement to check the variables

if (!empty($username) && !empty($passwordone) && !empty($name) && !empty($email) && !empty($age) && !empty($country)

How can I write this code so they wont be able to go through without assigning a date?

4 Answers4

1
$pattern = "^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$";
$date = $_POST['date'];
if (!empty($username) && !empty($passwordone) && !empty($name) && !empty($email) &&     !empty($age) && !empty($country) && !preg_match($pattern, $date)

Just add this to your validation and it should solved it

Ali
  • 3,479
  • 4
  • 16
  • 31
0

an simple alternative solution.

$date = $_POST['date']; 
$date='17-06-2013'; // example
list($d,$m,$y)= explode('-',$date);

if (!empty($username) && !empty($passwordone) && !empty($name) && !empty($email) &&     !empty($age) && !empty($country) && checkdate($m,$d,$y)===true
amigura
  • 541
  • 3
  • 6
0

I used a different method.

if(strlen($_POST['date']==10){

echo $_POST['date'];

}else{

echo "Please Select Date";

}
j08691
  • 204,283
  • 31
  • 260
  • 272
suresh
  • 73
  • 7
-1

Why not use isset($_POST['date'])?

If you want to validate the content of date as well, see Check if variable is a valid date with PHP

Community
  • 1
  • 1
heksemann
  • 436
  • 4
  • 3
  • Would this turn out the same as !empty? if I get a string value of 0 if nothing is changed that means it has been set. So in theory having !empty is safer. –  Jun 17 '13 at 11:28