Validation help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Validation help

Post 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?
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Validation help

Post 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
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Validation help

Post 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'
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Validation help

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Validation help

Post 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.
mikes1471
Forum Commoner
Posts: 88
Joined: Sat Jan 24, 2009 3:29 pm

Re: Validation help

Post 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'];
temidayo
Forum Contributor
Posts: 109
Joined: Fri May 23, 2008 6:17 am
Location: Nigeria

Re: Validation help

Post 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
    }
 
JasonDFR
Forum Commoner
Posts: 40
Joined: Wed Jan 07, 2009 3:51 am

Re: Validation help

Post 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;
Post Reply