php form validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runway27
    Banned
    New Member
    • Sep 2007
    • 54

    #1

    php form validation

    i need to validate a field in a form where a user enters a reference number this can be

    letters, numbers and special characters also so i have not written any special preg match as

    the username is a combination. the only check i am doing is if there are any white spaces

    and if a user simple presses the space bar and does not enter value i display a message to

    enter the reference number and even if there are white spaces followed by the reference

    number i have used trim method. i have checked in the database even if there are white

    spaces followed by reference number due to trim() method the data in the table is being

    inserted whithout those white spaces.

    following is the code i am presently using
    [code=php]
    $referencenumbe r = trim($_POST["referencenumbe r"]);

    if(strlen($refe rencenumber) == 0)
    {
    $error.="<li>Re ference number cannot be blank </li> <br />";
    }
    [/code]
    this code works perfectly fine and does what it is supposed to, however i am using

    techniques to avoid sql injection. following is the technique i have used
    [code=php]
    if(get_magic_qu otes_gpc())
    {
    $username = stripslashes($_ POST["username"]);
    }

    else
    {
    $username = $_POST["username"];
    }
    [/code]

    due to this even if i use
    [code=php]
    if(get_magic_qu otes_gpc())
    {
    $lodgementnumbe r = stripslashes($_ POST["lodgementnumbe r"]);
    }

    else
    {
    $lodgementnumbe r = trim($_POST["lodgementnumbe r"]);
    }

    if(strlen($lodg ementnumber) == 0)
    {
    $error.="refere nce number cannot be blank;
    }
    [/code]
    the validation is not doing what it does in the code i mentioned at the begining.

    i need to use techniques to avoid sql injection and i also need the validation to work.

    how can i fix this.

    please advice.

    thanks.
    Last edited by Atli; May 30 '08, 07:15 PM. Reason: Added [code] tags.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Runway.

    [code=php]
    $lodgementnumbe r = stripslashes($_ POST["lodgementnumbe r"]);
    [/code]

    No trim() in there, so whitespace is getting preserved.

    Comment

    Working...