Page 1 of 1

Validation help

Posted: Sun Mar 22, 2009 10:12 pm
by mikes1471
Hi guys

I have some validation errors on my registration page and having added dropdown options that include the values DD, MM and YYYY, obviously I'd like for my site visitors not to register with these values. Could someone tell me how I would pick up on this selection?

I guess its something like:

if($birthday == DD){
$errors[] = "Birthday not defined!";
}

Can anyone advise?

Re: Validation help

Posted: Mon Mar 23, 2009 7:24 am
by temidayo
You gave very few details about your script but I want to assume that
each of the DD,MM and YYYY are separate dropdown options.

If that is the case, they are probably sent through post method. You
can checked if something is selected by doint this:

Code: Select all

if isset($_POST['dd'])
To however validate the date, you can apply php function checkdate() on the three parameters

Re: Validation help

Posted: Mon Mar 23, 2009 6:34 pm
by mikes1471
Sorry I dont understand how I can use that

Yes I am using the POST method and my code for 'DD' for example is as follows:

Code: Select all

<select name="birthday">
        <option>DD</option>     //the line I donot wish the visitor to be able to select
        <option <?php echo ($birthday == '1')    ? 'selected' : ''; ?>>1</option>
        <option <?php echo ($birthday == '2')    ? 'selected' : ''; ?>>2</option>
    <option <?php echo ($birthday == '3')    ? 'selected' : ''; ?>>3</option>
    <option <?php echo ($birthday == '4')    ? 'selected' : ''; ?>>4</option>                        
    <option <?php echo ($birthday == '5')    ? 'selected' : ''; ?>>5</option>
        <option <?php echo ($birthday == '6')    ? 'selected' : ''; ?>>6</option>   
        <option <?php echo ($birthday == '7')    ? 'selected' : ''; ?>>7</option>       
        <option <?php echo ($birthday == '8')    ? 'selected' : ''; ?>>8</option>       
        <option <?php echo ($birthday == '9')    ? 'selected' : ''; ?>>9</option>       
        <option <?php echo ($birthday == '10')    ? 'selected' : ''; ?>>10</option>     
        <option <?php echo ($birthday == '11')    ? 'selected' : ''; ?>>11</option>
        <option <?php echo ($birthday == '12')    ? 'selected' : ''; ?>>12</option>             
        <option <?php echo ($birthday == '13')    ? 'selected' : ''; ?>>13</option>
        <option <?php echo ($birthday == '14')    ? 'selected' : ''; ?>>14</option>     
        <option <?php echo ($birthday == '15')    ? 'selected' : ''; ?>>15</option>     
        <option <?php echo ($birthday == '16')    ? 'selected' : ''; ?>>16</option>
        <option <?php echo ($birthday == '17')    ? 'selected' : ''; ?>>17</option>     
        <option <?php echo ($birthday == '18')    ? 'selected' : ''; ?>>18</option>     
        <option <?php echo ($birthday == '19')    ? 'selected' : ''; ?>>19</option>     
        <option <?php echo ($birthday == '20')    ? 'selected' : ''; ?>>20</option>     
        <option <?php echo ($birthday == '21')    ? 'selected' : ''; ?>>21</option>     
        <option <?php echo ($birthday == '22')    ? 'selected' : ''; ?>>22</option>     
        <option <?php echo ($birthday == '23')    ? 'selected' : ''; ?>>23</option>     
        <option <?php echo ($birthday == '24')    ? 'selected' : ''; ?>>24</option>     
        <option <?php echo ($birthday == '25')    ? 'selected' : ''; ?>>25</option>     
        <option <?php echo ($birthday == '26')    ? 'selected' : ''; ?>>26</option>     
        <option <?php echo ($birthday == '27')    ? 'selected' : ''; ?>>27</option>     
        <option <?php echo ($birthday == '28')    ? 'selected' : ''; ?>>28</option>     
        <option <?php echo ($birthday == '29')    ? 'selected' : ''; ?>>29</option>     
        <option <?php echo ($birthday == '30')    ? 'selected' : ''; ?>>30</option>     
        <option <?php echo ($birthday == '31')    ? 'selected' : ''; ?>>31</option>                     
        </select>
I apologise again for being a novice and the answer I was given was probably perfectly suitable but I'm unsure how to apply it to to my code.

Other validation clauses I have look like this:

Code: Select all

if(!$username){
            $errors[] = "Username is not defined!";
        }
        
        if(!$password){
            $errors[] = "Password is not defined!";
        }
and yes, the code I have for my 'DD' dropdown menu is used again for 'MM' and 'YYYY'

Re: Validation help

Posted: Tue Mar 24, 2009 4:49 pm
by mikes1471
OK, so I've tried this a couple of ways now:

Code: Select all

if ($birthday=="Day")
$errorstring= $errorstring."Birthday not defined!<br>";
 
if ($birthmonth=="Month")
$errorstring= $errorstring."Birthday not defined!<br>";
 
if ($birthyear=="Year")
$errorstring= $errorstring."Birthday not defined!<br>";
 
if ($errorstring)
echo "Fix the following errors:<p>".$errorstring;
And

Code: Select all

if ($birthday=="Day"){
        $errors[] = "Birthday not defined!";
    }
    
    if ($birthmonth=="Month"){
        $errors[] = "Birthday not defined!";
    }
 
    if ($birthyear=="Year"){
        $errors[] = "Birthday not defined!";
    }
Either way I try the registration goes ahead with the birthday values of Day/Month/Year and I really dont want it to, can anyone help in a way I understand lol

Re: Validation help

Posted: Tue Mar 24, 2009 5:51 pm
by jayshields
You aren't setting the values in any of your option tags. If you set your DD option to have the value DD then I guess you'll want to check that $_POST['birthday'] != 'DD'. You'll need to apply the same concepts to the birth month and birth year select boxes.

Also, you can concatenate strings onto strings using the $str .= 'some more stuff'; notation.

Re: Validation help

Posted: Tue Mar 24, 2009 9:45 pm
by mikes1471
OK so presumably I'd need to enter that data where I currently have this code?

Code: Select all

$birthdate = $_POST['birthday'] . "/" . $_POST['birthmonth'] . "/" . $_POST['birthyear'];

Re: Validation help

Posted: Wed Mar 25, 2009 3:08 am
by temidayo
To start with, your select code should look like this:

Code: Select all

 
<select name="birthday[]">
   <option>DD</option>     //the line I donot wish the visitor to be able to select
  //other options here
....
</select>
<select name="birthday[]">
   <option>MM</option>     //the line I donot wish the visitor to be able to select
   //other options here
....
</select>
<select name="birthday[]">
   <option>YYYY</option>     //the line I donot wish the visitor to be able to select
   //other options here
....
</select>
 
Once that has been POSTed, you can collect the collected data has follows:

Code: Select all

list($day, $month, $year) = $_POST['birthday'];
    
    $birthday = $year."/".$month."/".$day;//You can create any date format you desire here
 
    if (@!checkdate($month,$day,$year)){
        $err_msg =  "Incorrect date of deposit ".$birthday;
        //Take futher actions
    }
 

Re: Validation help

Posted: Wed Mar 25, 2009 3:16 am
by JasonDFR
Let me know if this works for you. The $_POST vars were just used for testing.

Code: Select all

<?php
$_POST['birthday'] = '33';
$_POST['birthmonth'] = '1';
$_POST['birthyear'] = '1970';
 
$errors = array();
 
if ( is_numeric($_POST['birthday'] . $_POST['birthmonth'] . $_POST['birthyear']) ) {
    if ( !checkdate($_POST['birthmonth'], $_POST['birthday'], $_POST['birthyear']) ) {
        $errors[] = 'Birthdate is not valid.';
    } else {
        $birthdate = $_POST['birthday'] . "/" . $_POST['birthmonth'] . "/" . $_POST['birthyear'];
    }
} else {
    $errors[] = 'Please enter your birthdate.';
}
 
echo $birthdate;
echo $errors[0];
 
exit;