Checking if Input is MM-DD

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iWizard
Forum Newbie
Posts: 6
Joined: Sun Jun 14, 2009 10:52 am

Checking if Input is MM-DD

Post by iWizard »

I have looked at a code snippet that was posted on the code snippet section of this forum and I tried to adapt it so that it would be MM-DD instead of YYYY-MM-DD but it has not been successful.

My Code Is:

Code: Select all

if(preg_match('/^(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $date)){
$datevalid=true;
}else{
$datevalid=false;
}
Return: Always true even when it does not match.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Checking if Input is MM-DD

Post by requinix »

I don't think the regex is at fault. Either $date isn't what you think it is or your code isn't "returning" correctly.

But anyways, what if they enter 02-31? Ain't valid. How about 02-29? Might be, depending on the year.
iWizard
Forum Newbie
Posts: 6
Joined: Sun Jun 14, 2009 10:52 am

Re: Checking if Input is MM-DD

Post by iWizard »

I know that $date is what I think it is because I added an extra line during testing to print out what the value had and it matched what I entered.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Checking if Input is MM-DD

Post by requinix »

And it doesn't have extra spaces or newlines attached to it? Put some characters around it (like "<" ">"), then pull up the HTML source of the page and make sure you see

Code: Select all

<MM-DD>
and not

Code: Select all

<MM-DD
>
And what about the code that returns the result? (Is it a function?) Have you checked that? How about posting the complete code and a more detailed explanation about what you're doing?
Post Reply