View Full Version : String length check failing

02-18-2010, 06:39 PM
Afternoon Everyone

I am having trouble validating the length of a variable returned from a form. The value received from the form appears to be correct, the results from strlen is within the required range but this check fails if the received input is numbers. If the input received from the form is alphanumeric, the function works correctly.

What am I missing?


if (isset($_POST['devid'])) {
$Device->device_id = stripString($_POST['devid'], 'device_id');
if (strlen($Device->device_id) < 4 || strlen($Device->device_id > 20)) {
$user_msg = "<li>Device ID is not valid ($Device->device_id). len=".strlen($Device->device_id)."</li>";
} else {

Output echoed to screen when it failed:

Device ID is not valid (0002310). len=7

The value of "0002310" is 100% correct. As you can see from the output line, post stripping it has maintained the leading zeroes and is 7 characters long - which should not fail the length check.

I have tried within the $Device object, to define the variable as string using:

$this->device_id = (string) $row->device_id;
to try to force it to work as a string.


02-18-2010, 06:57 PM

if (strlen($Device->device_id) < 4 || strlen($Device->device_id > 20)) {

if (strlen($Device->device_id) < 4 || strlen($Device->device_id) > 20) {
The > 20 needs to be outside of the strlen function

02-18-2010, 07:35 PM
Thank you!

I had a feeling it was one of those id10t type errors.