Aldayne_Henry Posted October 22, 2020 Share Posted October 22, 2020 sorry for the late reply @barand my internet is so shakey where i am at now. I want to ensure that the user enters the format for academic year example( 2020/21 ). But when i used the code below it allows for even letters into the mix as well and without brackets as well. How would i go about ensuring the user uses the format (2020/21) ... brackets included if(!empty($_POST ['ay'])) { if(!preg_match(" ([0-9/])",$_POST['ay'])) { $formerrors [] = ' " Incorrect format for academic year " '; } } Quote Link to comment https://forums.phpfreaks.com/topic/311626-enforce-format-for-academic-year/ Share on other sites More sharing options...
kicken Posted October 22, 2020 Share Posted October 22, 2020 If you want the entire string to be in that format, you need to use the start (^) and end ($) anchors and put your pattern in between. Your regex currently also only looks for digits and /, but they can be in any order. If you want to enforce that strict format you need to look 4 digits followed by a slash followed by 2 more digits. Given the above, you end up with a regex such as /^\(\d{4}\/\d{2}\)$/ 1 Quote Link to comment https://forums.phpfreaks.com/topic/311626-enforce-format-for-academic-year/#findComment-1582004 Share on other sites More sharing options...
Barand Posted October 22, 2020 Share Posted October 22, 2020 Personally, I wouldn't expect the user to enter the corrrect format. And even if the format is correct there is no guarantee that the value is valid E.G. (2020/19) or (2020/22) I would ask the user to enter the "2020" part and then generate the required format $input = 2000; $ay = sprintf("(%4d/%02d)", $input, ($input+1)%100); echo $ay; //--> (2000/01) My 0.02 worth. @kicken, I definitely come bottom of the class when it comes to regex and avoid it like the plague. However, as I was asked about this problem, I gave it go and eventually came up with a pattern of '#\(\d{4}\/\d{2}\)#' which appeared to do the job. My delimiters are different from yours, so is it wrong? 1 Quote Link to comment https://forums.phpfreaks.com/topic/311626-enforce-format-for-academic-year/#findComment-1582005 Share on other sites More sharing options...
kicken Posted October 22, 2020 Share Posted October 22, 2020 6 minutes ago, Barand said: My delimiters are different from yours, so is it wrong? The delimiters don't matter really, though by using # you could skip escaping the / in the expression. Normally I'd probably use something other than / as well in a situation like this just to avoid that escape, but I was to lazy to change them on the website. You need the start and end of string anchors though if you want to enforce the value strictly. Without them it just looks for the pattern somewhere in the string, not that the string is exactly that pattern. So your regex would also match if: $_POST['ay'] = 'I want to attend in the (2020/21) academic year'; 1 Quote Link to comment https://forums.phpfreaks.com/topic/311626-enforce-format-for-academic-year/#findComment-1582006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.