Prevent Continuous characters on a form - can you?

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

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Prevent Continuous characters on a form - can you?

Post by pickle »

coooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooool.

Yep, we do. It looks like the overflow property is what does it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Prevent Continuous characters on a form - can you?

Post by simonmlewis »

The problem with that is that if someone were to enter:
this is cooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooollllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll and I love it so much.

What would happen to the rest of it on that line?
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: Prevent Continuous characters on a form - can you?

Post by simonmlewis »

interesting. That's it then. Will try again and with the 'hidden' overflow. Thanks...
ssssssooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo much. :)
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Prevent Continuous characters on a form - can you?

Post by McInfo »

If you want to reject inputs with long words, you can use this pattern:

Code: Select all

if (preg_match('/[^-; \n]{32}/', $subject) === 0) {
    // All words have reasonable lengths (in this case, fewer than 32 characters).
} else {
    // At least one word is too long.
}
Or you can break long words with wordwrap(), but that would mean accepting long words and users might be annoyed by a "feature" that modifies their input.
Last edited by McInfo on Mon Apr 18, 2011 5:20 pm, edited 1 time in total.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Prevent Continuous characters on a form - can you?

Post by simonmlewis »

Genius. This is precisley what I was after.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Prevent Continuous characters on a form - can you?

Post by McInfo »

I did a little more testing. Firefox only wraps on a few characters. So, I've changed the pattern in my previous post.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Prevent Continuous characters on a form - can you?

Post by pickle »

If users are entering information in a textarea, another thing you may want to look at is changing how wordwrapping is done right in the textbox. Setting "wrap" to "hard" might be beneficial to the user as what they see is what gets posted & shown back to them.

http://www.tizag.com/htmlT/htmltextarea.php
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
darek
Forum Newbie
Posts: 8
Joined: Tue Apr 19, 2011 2:26 am
Location: Opole, Poland

Re: Prevent Continuous characters on a form - can you?

Post by darek »

The only way that worked for me is this:

Code: Select all

preg_replace ('/(?<=s|>)([^"'s<>]{59,})(?=s|<)/e', 'substr(chunk_split("$1", 57, "- "), 0, -2)', $str); 
Where:
- 59 is "max length of single word"
- 57 is "max length of single word" - 2
- $str is input string

How it works? Basically, it cuts long words into pieces and adds "- " at the end of each piece, except last piece.

You can also use php wordwrap function, but it looks good ONLY if you using fixed width fonts, so in most cases you don't want to use it.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Prevent Continuous characters on a form - can you?

Post by simonmlewis »

This looks to be a good method, but at the moment I am going to try the method from McInfo, where it just tells them off if they have entered too many non-stop characters. As I prefer to tell them it's wrong, rather than just ignore it and let them think it's wrong.

Or do you think I should just make them think it has passed through? Which is kind of what I am doing now - it just doesn't go live.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
darek
Forum Newbie
Posts: 8
Joined: Tue Apr 19, 2011 2:26 am
Location: Opole, Poland

Re: Prevent Continuous characters on a form - can you?

Post by darek »

That's great question, simonmlewis. I see that you are one of these guys who like to think far beyond "here and now". Answering your question: I must admint, it's better idea to tell people they have entered too many characters in one word. You should use this method wherever you can.

But sometimes you already have database full of such too long words. That's where my method will work better.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Prevent Continuous characters on a form - can you?

Post by simonmlewis »

True - but we moderate everything, so those long words never get thru. That's half the problem - when we moderate, and someone enters vastly long words, the moderation page goes 'wide'.

So this script will stop that properly, and keep the user informed.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply