Page 1 of 1
Validating textarea -> TEXT mysql column
Posted: Fri May 05, 2006 8:44 pm
by seodevhead
When I validate a form field input for something like a "first name", that is to be put in a "first_name" MySQL database column of VARCHAR(15) type... I usually validate the form field by doing something like this:
Code: Select all
if(isset($_POST['firstName']) && is_string($_POST['firstName'] && strlen($_POST['firstName']) <= 15)
{
echo 'Validated';
}
But how can I validate the length of a textarea input in a form that is to be put in a TEXT type MySQL column? Should I do this?
Code: Select all
if(strlen($_POST['textArea']) <= 65535)
65535 because that is the character max of the TEXT MySQL column-type. Is this advisable to do as far as length checking? Or is this overkill for something like a textarea? Thanks for any advice you can give! Take care.
Posted: Fri May 05, 2006 10:49 pm
by feyd
MySQL will automatically clip the text if it exceeds the length allowed.
Posted: Sat May 06, 2006 2:35 am
by s.dot
Depends on how many characters you want to allow.
If you want to allow 65,353 or whatever characters then yes, mysql will clip it for you (but that's a lot of characters!)
If you want to limit it, do something like :
Code: Select all
$text = substr($_POST['text'],0,15000);
Posted: Thu May 11, 2006 6:34 pm
by Technocrat
Probably better to write a function to validate this, since I am betting your going to do this more than once.
Something like:
Code: Select all
function stringValidate($string, $size=65535) {
if(isset($string) && is_string($string) && !empty($string) && strlen($string) <= $size) return true;
return false;
}
stringValidate($_POST['text']);
Posted: Fri May 12, 2006 1:59 am
by matthijs
If I may ask: what is the reason to use is_string in this case? Is it an extra measure? Is the POST data (or GET if one uses that) not always a string?
(just curious, as I haven't used that function before)
Posted: Fri May 12, 2006 2:03 am
by RobertGonzalez
I would guess he uses is_string to make sure it is not an array? Just a guess.
Posted: Fri May 12, 2006 2:12 am
by matthijs
Yes, of course. But maybe I should refrase my question then: what happens if you leave that out? Isn't a POST variable (if you use the function for that) not always a string? What happens if you leave the is_string out and feed the function an array? Aargh, too many question

I'll have to do some research myself I guess

Posted: Fri May 12, 2006 2:54 am
by Maugrim_The_Reaper
No reference to empty()...
Should use ctype_* family of functions if it's a string with a specific restraint - alphanumeric/alphabetic/numeric (whole number)/etc.
Posted: Fri May 12, 2006 5:21 am
by Weirdan
Isn't a POST variable (if you use the function for that) not always a string?
Element of the POST array could be an array itself.