views:

1488

answers:

5

I'm trying to create a multiline textarea in a php page, and I want to validate so that if the user can't insert more than 50 characters per line, or more than 50 lines. The idea is that the user can paste something from a spreadsheet, but if one line has more than 50 characters, the rest will be discarded, and not inserted in the next line. That's why I want to avoid the idea of having 50 individual textboxes.

It would be ideal if this could be done in javascript (or php itself, but I didn't saw anything like it in php).

Thanks!

UPDATE: Thanks for all that answers, but that would work only after the user submitted the form, right? For example, if the max lines is 3 instead of 50, and the user inserts 100 consecutive characters, and then a line break, it would only have 50 characters left, limiting the inputs to 2 instead of 3. Hope I was clear...

A: 

Use explode("\n", $str) to break the textarea data into lines, then check the length of each line.

A similar method could be used on the user end with Javascript, but if you want to ensure security you must do it on the PHP end because Javascript can be circumvented.

Ultimately it depends on whether this is to make the user's life easier or to prevent bad things happening.

Artelius
A: 

PHP:

$max_length = 50;
$lines = explode("\n", $input);
for($i = 0; $i < count($lines); $i++)
{
    $lines[$i] = rtrim($lines[$i]); // just incase there's a "\r"
    $lines[$i] = (strlen($lines[$i]) <= 50 ? 
                      $lines[$i] : 
                      substr($lines[$i], 0, 50));
}
$input = implode("\n", $lines);
Matthew Scharley
You have a bug, it's rtrim() not trimr().
Alix Axel
+1  A: 

Here it is, based on the code provided by Matthew, but this is shorter and also limits the number of lines.

$lines = array_slice(explode("\n", $string), 0, 50); // max 50 lines

foreach ($lines as $key => $value)
{
    $lines[$key] = substr(trim($value), 0, 50); // max 50 chars
}

$string = implode("\n", $lines);
Alix Axel
A: 

Find length of longest line:

$length = array_reduce(array_map(explode("\n", $string), 'strlen'), 'max', 0);
soulmerge
+1  A: 

You could break this down into two processes:

1: Use jQuery (or what ever JavaScript library you like) to validation the 50 character line limit for each line. The validation plugin offers a lot of functionality that might be useful.

2: Use php to double check just in case the use disables the JavaScript on the browser side. This would have to be the on submit functionality due to JavaScript being disabled (So no Ajax)

So what you would have to do is read the text area into an array using the line break \n to split the text into each element in the array. Now check each array element for the 50 character limitation. And now that you have all the text into an array you can also check the rows as well by array size.

Phill Pafford
The submit uses a javascript to change a flag, and the action is on the same page.I don't think there is any problem if the javascript is deactivated, the page won't work.
nightshadex101
@nightshadex101, I think you and I have different definitions of 'don't think there is any problem.' If your users experience a broken page why would they come back to you, do you have a captive audience somehow? O.O
David Thomas
I agree with @ricebowl, the user should experience the same (if not very close) action if Javascript is enabled or not, this is why you would do the backend check/functionality as well. Another approach (which is not recommended but could be done) is to display a message to the end user that they need to enable JavaScript if they have it disabled and display the correct page if it is enabled. Still I would highly recommend against this approach as it is not good coding practice as well as user experience.
Phill Pafford
Yes, I have a captive audience, as this is a tool that must be used by a small group of people. They must use this, they don't have any other choices.
nightshadex101
I understand that the users must use your tool but they don't have to have a bad experience using it. I guess it's how you want people to view your coding skills. Do you want them to think that the user experience is okay but the website doesn't work sometimes as expected or that you covered all your bases and the script works and has a great user experience. If this is for a selected audience don't you think they will review and rate your work? Just my two cents
Phill Pafford