SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I write this validation function..help

    Hello,

    I am trying to write a function which validates a name field. Code work on its own but once I place it as function, it doesn't work.

    PHP Code:

    <?php

    ini_set
    ('display_errors'1);
    error_reporting(E_ERROR);
        
        
    $fname = ($_POST['name']);
        
    $error = array();
        
        function 
    check($val)
        {    
            if (
    is_numeric($val))
            {
            
    $errors[] = 'Please Enter a Valid Name';
            }
            if(
    strlen($val) < 6)
            {
            
    $errors[] = 'Name must be at least 6 characters long';
            }
        }
        
    if (isset(
    $_POST))
            {
            
    check($fname);
            }    

    if(
    count($errors)) 
        {
            echo 
    '<div class="errors"><ul>';
            foreach(
    $errors as $error)
            {
            echo 
    '<li>'.$error.'</li>';
            }
            echo 
    '</ul></div>';
        }     
    ?>

  2. #2
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a scope problem. The $errors variable in your function is different to the one outside your function.

    At the start of the function add

    PHP Code:
    global $errors
    All should work then.
    PHP | MySQL | (X)HTML | CSS

  3. #3
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks Brent

    I made the $errors global and still no show. here is the code:

    PHP Code:

    <?php

    ini_set
    ('display_errors'1);
    error_reporting(E_ERROR);
        
        
    $fname = ($_POST['name']);
        global 
    $errors;
        
        function 
    check($val)
        {    
            if (!
    is_numeric($val))
            {
            
    $errors[] = 'Mobile must contain a number with no spaces or punctuation';
            }
            if(
    strlen($val) < 6)
            {
            
    $errors[] = 'Mobile must be at least 6 characters long';
            echo 
    $erros;
            }
        }
        
    if (isset(
    $_POST))
            {
            
    check($val);
            }    

    if(
    count($errors)) 
        {
            echo 
    '<div class="errors"><ul>';
            foreach(
    $errors as $error)
            {
            echo 
    '<li>'.$error.'</li>';
            }
            echo 
    '</ul></div>';
        }     
    ?>

  4. #4
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, no, you should place 'global $errors;' to your function, to its first line.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you should. Using globals is bad practice, make your function return a value, as all good functions should.

    PHP Code:
    function check($val)
    {    
        
    $errors = array();
        if (!
    is_numeric($val))
            
    $errors[] = 'Mobile must contain a number with no spaces or punctuation';
        if(
    strlen($val) < 6)
            
    $errors[] = 'Mobile must be at least 6 characters long';
        
    // ... more checks
        
    return $errors;


  6. #6
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cannot display the errors

    Thanks stereofrog for your help. I tried your code which seems all good but I cannot seem to display errors. Here is my simplified code which still doesn't display errors:

    PHP Code:

    <?php

    ini_set
    ('display_errors'1);
    error_reporting(E_ERROR);
        

        function 
    check($fname)
        {
            
    $errors = array();
            
            if(
    is_numeric($fname))
                {
                
    $errors[] = 'NUMBERIC';
                return 
    $errors;
                }
        }
              
        if (isset(
    $_POST))
            {
            
    check($fname);
            }    

        if(
    count($errors)) 
        {
            echo 
    '<div class="errors"><ul>';
            foreach(
    $errors as $error)
            {
            echo 
    '<li>'.$error.'</li>';
            }
            echo 
    '</ul></div>';
        }     
    ?>

  7. #7
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're returning the errors from the function, but not storing the return anywhere, try this...
    PHP Code:

    <?php
    ini_set
    ('display_errors'1); 
    error_reporting(E_ERROR); 
         
        
    $fname = ($_POST['name']); 
        
    $error = array(); 
         
        function 
    check($val
        {     
            if (
    is_numeric($val)) 
            { 
            
    $errors[] = 'Please Enter a Valid Name'
            } 
            if(
    strlen($val) < 6
            { 
            
    $errors[] = 'Name must be at least 6 characters long'
            } 
        return 
    $errors;
        } 
         
    if (isset(
    $_POST)) 
            { 
            
    $checker=check($fname); 
            }     

    if(
    count($checker))  
        { 
            echo 
    '<div class="errors"><ul>'
            foreach(
    $checker as $error
            { 
            echo 
    '<li>'.$error.'</li>'
            } 
            echo 
    '</ul></div>'
        }
    ?>
    Moving slightly off-topic, making sure the input isn't numeric is nowhere close to securing your script from hacking, if that's what you were aiming to do.
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  8. #8
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    NYC
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey bigalreturns,

    Thanks a lot for your help. Your codes does work but there is an issue. The issue is that when I load the page there is already an error message:

    Please Enter a Valide Name.

    Why is this error messege showing even if the form is not submitted?

    Thanks.

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the problem is here
    PHP Code:
    if (isset($_POST)) 
    $_POST is always set, regardless of request method used. You probably want to check the latter directly:
    PHP Code:
    if($_SERVER['REQUEST_METHOD'] == 'POST'


Bookmarks

Posting Permissions

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