By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,034 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,034 IT Pros & Developers. It's quick & easy.

How to use what the function returns?

P: n/a
Hi

I am a newbie and struggling with my guestbook validation process.
I have found the following function online to check the length of a
form field.

<?php
function checkLength($string, $min, $max) {
$length = strlen ($string);
if (($length < $min) || ($length > $max)) {
return FALSE;
} else {
return TRUE;
}
}
?>

Now below is the area in my code where I have to fit what that function
returns but I can't do it. The "Text" field is the one that has to be
checked for a certain length (Like $min = 1, $max = 100).I would like
to point to the error.html file if checkLength returns false.

$Text = $_POST['Text'] ;
$email = $_POST['email'] ;
$name = $_POST['name'] ;

if (empty($name) || empty($email)) {
header( "Location: error.html" );

}
else
{

//The fields are entered in the database here if it all goes fine.
Thanks a lot

Patrick

Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
varois83 wrote:
Now below is the area in my code where I have to fit what that
function returns but I can't do it. The "Text" field is the one that
has to be checked for a certain length (Like $min = 1, $max = 100).I
would like to point to the error.html file if checkLength returns
false.

$Text = $_POST['Text'] ;
$email = $_POST['email'] ;
$name = $_POST['name'] ;

if (empty($name) || empty($email)) {
header( "Location: error.html" );


if (!checkLength($Text, 1, 100) || empty($name) || empty($email)) {
....
}

BTW, there's more to form validation than used in this code (per example, it
will allow a single white space character to pass as a valid email address),
but you will probably get to this when you have learned more about PHP.
JW

Jul 17 '05 #2

P: n/a
I noticed that Message-ID: <41**********************@news.euronet.nl>
from Janwillem Borleffs contained the following:
if (!checkLength($Text, 1, 100) || empty($name) || empty($email)) {

Bit unfriendly sending people to an error page if they type too much.
Still I suppose you have your reasons. Consider using some Javascript
in addition to the PHP code to give the user instant feedback that they
are over the limit
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3

P: n/a
On Sun, 23 Jan 2005 23:14:54 +0000, Geoff Berrow <bl@ckdog.co.uk> wrote:
I noticed that Message-ID: <41**********************@news.euronet.nl>
from Janwillem Borleffs contained the following:
if (!checkLength($Text, 1, 100) || empty($name) || empty($email)) {


Bit unfriendly sending people to an error page if they type too much.
Still I suppose you have your reasons. Consider using some Javascript
in addition to the PHP code to give the user instant feedback that they
are over the limit


Or the maxlength attribute if it's an <input>. Shame there isn't such an
attribute on <textarea> though.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4

P: n/a
Hi

JB

God I tried everything for 2 hours, it's almost not fair.A pair of
parenthesis did me in.
Look at what I had that came the closest.:

if (empty($name) || empty($email) || (!checkLength($Text, 1, 100))) {

I also understand that there is a lot more to validation than that but
practice will make perfect.

Thanks so much for your help JB it's working now.

Geoff

Bit unfriendly sending people to an error page if they type too much.
Still I suppose you have your reasons. Consider using some Javascript
in addition to the PHP code to give the user instant feedback that they
are over the limit.

Just playing around with the code at this time Geoff, trying to use
only PHP at this point. I am going to create a page that tells people
that use more than 100 characters not to do so.
Thanks a lot guys

Patrick

Jul 17 '05 #5

P: n/a
JW

I spoke too fast in my previous post and tested what you said about the
code allowing a single space. It did!
How do you fix that?

Thanks again

Patrick

Jul 17 '05 #6

P: n/a
varois83 wrote:
JW

I spoke too fast in my previous post and tested what you said about the
code allowing a single space. It did!
How do you fix that?

Thanks again

Patrick


regular expressions
Jul 17 '05 #7

P: n/a
eregi()

http://us2.php.net/manual/en/function.eregi.php
It takes time to master.

Jul 17 '05 #8

P: n/a
Thanks again guys will get on it right away.

Patrick

Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.