strlen() function problem

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
sparkyjoe
Forum Newbie
Posts: 2
Joined: Tue Nov 25, 2008 9:40 pm

strlen() function problem

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: strlen() function problem

Post 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:
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: strlen() function problem

Post 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
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: strlen() function problem

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: strlen() function problem

Post by aceconcepts »

Can't believe i didn't spot the semi-colon. However, this should throw a self explanatory error.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: strlen() function problem

Post 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()
{
}

:-)
sparkyjoe
Forum Newbie
Posts: 2
Joined: Tue Nov 25, 2008 9:40 pm

Re: strlen() function problem

Post 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?
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: strlen() function problem

Post 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
Post Reply