Jump to content

Recommended Posts

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 " ';
			}
		}

 

Link to comment
https://forums.phpfreaks.com/topic/311626-enforce-format-for-academic-year/
Share on other sites

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}\)$/

 

  • Great Answer 1

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?

  • Great Answer 1
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';

 

  • Thanks 1
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.