I feel like this is supposed to be pretty easy and straight forward although somehow im getting the logic wrong.
function postLengthTitle($post_title_test) {
$result;
$title = strlen($post_title_test);
if (!$title < 40 || !$title > 200) {
$result = true;
} else {
$result = false;
return $result;
}
}
function postLengthContent($content_test) {
$result;
$content = strlen($content_test);
if (!$content < 500 || !$content > 2000) {
$result = true;
} else {
$result = false;
return $result;
}
}
if(postLengthTitle($post_title_test) === false){
header("location: ../content/makeapost.php?".htmlspecialchars($postpage2)."");
exit();
}
if(postLengthContent($content_test) === false){
header("location: ../content/makeapost.php?".htmlspecialchars($postpage3)."");
exit();
}
The title works fine although the content does not. Giving 500 characters still gives me the error. Is this the correct way of validating the character length input?
CodePudding user response:
Well, it's hard to recode code that makes little sense, but here's my best attempt:
function validLengthTitle($title)
{
$length = strlen($title);
return ($length >= 40) && ($length <= 200);
}
function validLengthContent($content)
{
$length = strlen($content);
return ($length >= 500) && ($length <= 2000);
}
if(!validLengthTitle($post_title_test)){
header("location: ../content/makeapost.php?".htmlspecialchars($postpage2)."");
exit();
}
if(!validLengthContent($content_test)){
header("location: ../content/makeapost.php?".htmlspecialchars($postpage3)."");
exit();
}
I cannot, in no way, guarantee that this code does what you want it to do.
If you're going to make more length checks you could create a more general function:
function validateLength($text, $minLength, $maxLength)
{
$length = strlen($text);
return ($length >= $minLength) && ($length <= $maxLength);
}