Page 1 of 1

Checking if Input is MM-DD

Posted: Wed Jun 17, 2009 7:47 pm
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.

Re: Checking if Input is MM-DD

Posted: Wed Jun 17, 2009 7:52 pm
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.

Re: Checking if Input is MM-DD

Posted: Wed Jun 17, 2009 7:55 pm
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.

Re: Checking if Input is MM-DD

Posted: Wed Jun 17, 2009 9:56 pm
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?