Page 1 of 1

strlen() function problem

Posted: Wed Nov 26, 2008 5:26 am
by sparkyjoe
Hi everyone. I need some help as I am about to pull out what's left of my hair. I'm not sure what I'm doing wrong but I can't seem to figure out how to use the strlen() function for what I want to do.
I have a form with a user input box for entering a date in the format of MM/DD/YYYY and store it in a variable called $date. Once the user submits the information I use my php script to process and validate the information he/she has entered. Well I'm trying to use the strlen() function to verify that the user has entered the correct length string which I assume would be a string length of ten.

Code: Select all

<?php
if (strlen($date) < 10 );
{
echo "Please enter correct date format";
{
else
}
// continue processing form;
}
?>
Well once my php script processes it I get the same result whether i have entered the correct string length or no text at all.
Could someone please help point me in the right direction or give me an example of what they think the code should be structured to complete the task of checking the user input?
Thanks in advance everyone.

Re: strlen() function problem

Posted: Wed Nov 26, 2008 5:29 am
by aceconcepts
How have you formated $date?

Also, i'd name the variable as $thisDate or $strDate just so no conflicts are caused later on down the line :wink:

Re: strlen() function problem

Posted: Wed Nov 26, 2008 5:34 am
by mintedjo
This might be one of the most epic failures ever!

But its probably just this very common typo.

Code: Select all

if (strlen($date) < 10 );<-- remove me
I've fallen for that in the past...

Hope that solves it for you

Re: strlen() function problem

Posted: Wed Nov 26, 2008 5:39 am
by pcoder
You can find out the error if you go through your code thoroughly.
In my opinion,it would be better to use regular expression to validate the date format.
Cheers

Re: strlen() function problem

Posted: Wed Nov 26, 2008 5:42 am
by aceconcepts
Can't believe i didn't spot the semi-colon. However, this should throw a self explanatory error.

Re: strlen() function problem

Posted: Wed Nov 26, 2008 6:26 am
by papa
mintedjo wrote:This might be one of the most epic failures ever!

But its probably just this very common typo.

Code: Select all

if (strlen($date) < 10 );<-- remove me
I've fallen for that in the past...

Hope that solves it for you
One of the reasons I prefer to code my functions and if statements:

if() {

}

function() {
}

instead of

if()
{
}

:-)

Re: strlen() function problem

Posted: Wed Nov 26, 2008 9:33 am
by sparkyjoe
How have you formated $date?
No I haven't even got that far. Would the following be a better validation than the strlen() function?
Please have patience with me. I am somewhat new to PHP and an still learning.
$string = "";
if (preg_match('/^\d{1,2}\/\d{1,2}\/\d{4}$/', $string)) {
// Continue procesing data;
}
else
{
echo "Please correct the date";
}
What would go between the quotes? would it be the variable $date that is set with the input box from the form?

Re: strlen() function problem

Posted: Wed Nov 26, 2008 9:52 am
by mintedjo
sparkyjoe wrote:Would the following be a better validation than the strlen() function?
yes
sparkyjoe wrote: ...an still learning.
aren't we all
sparkyjoe wrote:would it be the variable $date that is set with the input box from the form?
Sounds good to me