Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post

    String length check failing

    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?

    Code:
    PHP Code:
    if (isset($_POST['devid'])) {
        
    $Device->device_id stripString($_POST['devid'], 'device_id');
        if (
    strlen($Device->device_id) < || 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:
    PHP Code:
    $this->device_id    = (string) $row->device_id
    to try to force it to work as a string.

    Thanks.
    Alan
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    change
    PHP Code:
    if (strlen($Device->device_id) < || strlen($Device->device_id 20)) { 
    to
    PHP Code:
    if (strlen($Device->device_id) < || strlen($Device->device_id) > 20) { 
    The > 20 needs to be outside of the strlen function
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    Nightchild (02-18-2010)

  • #3
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post

    Resolved

    Thank you!

    I had a feeling it was one of those id10t type errors.
    Of all the things I've lost in my life time, my mind is the one I miss the most!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •