How do I stop someone posted spaces in a form?

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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I stop someone posted spaces in a form?

Post by simonmlewis »

We are finding people are jsut tapping space bar and hitting the Submit button.
It's therefore not NULL, but does that mean it is "empty"?

How do I validate in PHP against spaces?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I stop someone posted spaces in a form?

Post by Celauran »

preg_replace spaces and check that length is still > 0? That does nothing to address the larger problem, though. Why are your users doing that?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I stop someone posted spaces in a form?

Post by simonmlewis »

What's the different between that, and empty(trim($var)) ??
People are possibly clicking reply twice, as the page turns over so quickly.
At first, this was caused by people replying, and my not doing validation.
And because I don't think you can validate a <textarea> onsubmit, to ensure something is entered, they went thru blank.

I added a script to stop them if they are "empty", but spaces got thru. So I think TRIM works. As effectly as your version..... any ideas?

How do I check length?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I stop someone posted spaces in a form?

Post by Celauran »

simonmlewis wrote:How do I check length?
strlen()

I agree, trim should work just as well.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I stop someone posted spaces in a form?

Post by simonmlewis »

Not to worry, this one is very cool:

Code: Select all

http://stanislav.it/javascript-display-content-with-delay/
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I stop someone posted spaces in a form?

Post by simonmlewis »

The TRIM doesn't work.
I get this error in my logs:
[Tue Jun 03 09:31:41 2014] [warn] [client 92.14.61.124] mod_fcgid: stderr: PHP Fatal error: Can't use function return value in write context in /var/www/vhosts/site.com/httpdocs/includes/ticket.inc on line 171, referer: http://www.site.com/tickets
Line 171 is that line:

Code: Select all

if (empty(trim($message)))
{
}
Am I writing it wrong??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I stop someone posted spaces in a form?

Post by Celauran »

Yes, you have to assign it to a variable before passing it to empty()

Code: Select all

$message = trim($message);
if (empty($message)) {
... etc
Post Reply