bschultz Posted May 20, 2010 Share Posted May 20, 2010 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 https://forums.phpfreaks.com/topic/202341-question-about-isset/ Share on other sites More sharing options...
ScotDiddle Posted May 20, 2010 Share Posted May 20, 2010 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 https://forums.phpfreaks.com/topic/202341-question-about-isset/#findComment-1061101 Share on other sites More sharing options...
foxsoup Posted May 20, 2010 Share Posted May 20, 2010 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 https://forums.phpfreaks.com/topic/202341-question-about-isset/#findComment-1061117 Share on other sites More sharing options...
gwolgamott Posted May 20, 2010 Share Posted May 20, 2010 I always favor using both server side for final check incase js is disabled for security reasons & client side for a good user experience. Link to comment https://forums.phpfreaks.com/topic/202341-question-about-isset/#findComment-1061120 Share on other sites More sharing options...
bschultz Posted May 20, 2010 Author Share Posted May 20, 2010 Thanks much Guys! Link to comment https://forums.phpfreaks.com/topic/202341-question-about-isset/#findComment-1061153 Share on other sites More sharing options...
bschultz Posted May 20, 2010 Author Share Posted May 20, 2010 post removed by me...the dumbie! Link to comment https://forums.phpfreaks.com/topic/202341-question-about-isset/#findComment-1061162 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.