Jump to content

Question about isset


bschultz

Recommended Posts

I have the following code...which works...but I'd like to check it so that the user can't type in anything they like...and create errors.

 

The code below will set a variable in a php calendar.

 

Obviously, I need $thismonth and $thisyear to be 01-12 and and valid year respectively. 

 

Is there a way to check this, so that only months and years will be accepted?

 

Thanks!

 

<?php
//BEGIN MONTH ISSET
if(isset($_GET['thismonth']))
          {
          $thismonth = $_GET['thismonth'];
    }
elseif(!isset($_GET['thismonth']))
          {
          $thismonth = date('m');
    }
//BEGIN YEAR ISSET	
if(isset($_GET['thisyear'])) 
          {
          $thisyear = $_GET['thisyear'];
    }
elseif(!isset($_GET['thisyear']))
          {
          $thisyear = date('Y');
    }	
echo "$thismonth<br />$thisyear";
?>

Link to comment
Share on other sites

bschultz,

 

You could call javascript during an onBlur event for each input field, and check the value entered there.  Then either allow the data entry to stand, or set it to null if in error and send an alert to the users...

 

There are lots of JS validation scripts out there which Google can find for you.

 

Scot L. Diddle, Richmond VA

Link to comment
Share on other sites

It would be better to validate the user input on the server side to prevent people with disabled Javascript getting around your validation.  Assuming that the month is in two-digit format and between 01 and 12, and the year is in four-digit format, you could use this code:

 

<?php

// Set month and year from $_GET variables if they exist, otherwise generate current
// month and year from date() function
$thismonth = isset($_GET['thismonth']) ? $_GET['thismonth'] : date('m');
$thisyear = isset($_GET['thisyear']) ? $_GET['thisyear'] : date('Y');

// Validate month
if (preg_match('/^[0-1][0-9]$/', $thismonth) === 0 || $thismonth < 1 || $thismonth > 12) {
echo 'Error in month';
} else {
echo "Month: $thismonth";
}

echo '<br />';

// Validate year
if (preg_match('/^[0-9]{4}$/', $thisyear) === 0) {
echo 'Error in year';
} else {
echo "Year: $thisyear";
}

?>

 

Both the month and year validations use regular expressions to ensure that the correct number of digits (and only digits - not other characters) exists.  The month validation also checks that the month is between 01 and 12.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.