adam291086 Posted December 18, 2007 Share Posted December 18, 2007 I have some script that is taking an entered day dd/mm/yyyy. Spliting it up into dd mm and yyyy. Then checking if dd/mm/yyyy is entered in with /. then i am trying to check if it contains number. All i get is the echoed error message. Can anyone point me in the right direction <?php if ($_POST) { error_reporting(E_ALL); //get the variables $title = $_POST['title']; $description = $_POST['description']; $date = $_POST['date']; //search for / in day, month and year $findme = '/'; $date1 = strpos($date, $findme); // Nope, 'a' is certainly not in 'xyz' if ($date1 === false) { echo "The date must have the format dd/mm/yyyy"; } if ($date1 !== false) { //get the day, month and year $date = explode("/", $date); $day = $date[0]; $month = $date[1]; $year = $date[2]; } if (is_numeric($day && $month && $year)) { echo "Event Added Successfully"; } else { echo "Invalid date entry. Please try again"; } //end if at start } ?> I have done some debugging and all the vairbles from the form are being posted Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/ Share on other sites More sharing options...
helraizer Posted December 18, 2007 Share Posted December 18, 2007 Try <?php if (is_numeric($day) && is_numeric($month) && is_numeric($year)) { echo "Event Added Successfully"; } else { echo "Invalid date entry. Please try again"; } ?> That should work. Sam Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417611 Share on other sites More sharing options...
adam291086 Posted December 18, 2007 Author Share Posted December 18, 2007 oh so simple. Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417616 Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 You would be much better off using one simple regex. I'm not much good at them, but this might go close. Ask your question in the regx board if you get stuck. <?php $date = $_POST['date']; if (preg_match('/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/',$date) { // valid. } ?> Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417617 Share on other sites More sharing options...
Daniel0 Posted December 18, 2007 Share Posted December 18, 2007 You might want to use /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/ instead so you're sure there isn't anything before or after as well. Or /^\d{2}\/\d{2}\/\d{4}$/ (\d means any digit, i.e. [0-9]). Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417621 Share on other sites More sharing options...
adam291086 Posted December 18, 2007 Author Share Posted December 18, 2007 ok i will look into regex. Never used them before but seen some people using them. Thanks guys Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417627 Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 That was the same format I used. I can give you the regex I used if you want. Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417647 Share on other sites More sharing options...
adam291086 Posted December 18, 2007 Author Share Posted December 18, 2007 yes please. As the manual only gives the basics. Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417648 Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Whats wrong with Daniel0's example? <?php $date = $_POST['date']; if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/',$date) { // valid. } ?> Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417652 Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 Here is the one I used ^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/(\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/(\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/(\d{2}))|(29\/02\/((0[48]|[2468][048]|[13579][26])|(00))))$ Found at http://regexlib.net/REDetails.aspx?regexp_id=488 Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417654 Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 What if I enter 44/44/4444 ? Whats wrong with Daniel0's example? <?php $date = $_POST['date']; if (preg_match('/^\d{2}\/\d{2}\/\d{4}$/',$date) { // valid. } ?> Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417659 Share on other sites More sharing options...
PFMaBiSmAd Posted December 18, 2007 Share Posted December 18, 2007 Actually, if you add the 3rd parameter to the preg_match() function to get it to return the fields it matches in an array, you can use the checkdate() function to confirm the date is valid - http://php.net/checkdate Link to comment https://forums.phpfreaks.com/topic/82187-solved-is_numeric-not-working/#findComment-417666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.