1

I am working on creating an array of regular expressions based on form values and using a function that fails on the wrong user input. Every time I run the website I receive the following error:

Warning: eregi() [function.eregi]: REG_EMPTY

I do not know what is wrong. Please take a look at my code and help. Thank you!

$error_log = array();
// Checks if user inputed data matches the default values
$arr = array( 
        'name' => 'First Name Last Name', 
        'month' => 'MM', 
        'day' => 'DD',
        'year' => 'YYYY',
        'address1' =>'Address Line 1',
        'address2' => 'Address Line 2',
        'email' => '[email protected]'
        );

$regex = array(
        'name' => "^[a-z .'-]+$", 
        'month' => "^((0[1-9])|(1[0-2]))$ ", 
        'day' => "0?[1-9]|[1-2][0-9]|3[0-1]", 
        'year' => "^(19|20)\d{2}$",
        'address1' => "/^[a-zA-Z0-9 ]*$/",
        'address2' => "/^[a-zA-Z0-9 ]*$/",
        'email' => "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"
        );
/*
Runs validation on the form values and stops procesing if the form does not have the correct values
*/
function regexValidate( $form_value, $regex, $key){
    if(!eregi($regex[$key],$form_value) ){
        return true;    
    }
    return false;
}
3
  • 3
    (sidenote) As of PHP 5.3.0, the eregi is deprecated in favor of the PCRE extension. Calling this function will issue an E_DEPRECATED notice. See the list of differences for help on converting to PCRE. Commented Feb 14, 2011 at 16:40
  • 1
    I suggest you try outputting $key and $regex[$key] to see what regex you're attempting to use. If there's a problem there (say, an unexpected key value) you're going to have a null regex fed to eregi(), causing your error. As Gordon points out, eregi() is deprecated. I think preg_match() will be a better choice. Commented Feb 14, 2011 at 16:40
  • 3
    The question cannot be answered if you dont show how you feed the values to regexValidate. Commented Feb 14, 2011 at 16:43

1 Answer 1

2

I have a slightly different approach. I have an array such as this - which I ultimatley pass to Smarty to build my for - but upon submission, this array is passed witht he form $_POST into a function that loops round and does the necessary validation (based on the 2nd parameter here);

$arrFields = array(
                'stage1' => array(
                    'accountname'           =>  array('Account Name','text','text','stage1',true),
                    'presharedaccountkey'   =>  array('Pre-shared Key','text','text','stage1',true),
                    'ipwhitelist'           =>  array('IP White-list','text','text','stage1',false),
                    'accountid'             =>  array('Customer ID','text','readonly','stage1',false),
                    'product'               =>  array('Product','text','select','stage1',false,$platformProducts),
                    'dtcreated'             =>  array('Created on','text','text','stage1',false),
                    'createdby'             =>  array('Created By','text','text','stage1',false),
                    'mode'                  =>  array('Mode','text','radio','stage1',true,$platformModes)
                )
            );

i do some custom stuff but basically to loop it'd be:

function validateFormPost($arrFields,$postfields)
{
    $err = array();
    //validate required fields
    foreach($arrFields as $stagekey => $stageval)
    {
        foreach($stageval as $key => $val)
        {
            // your validation based on field type
            // i.e. in here would be regex to deal with std type or indeed you could add it as a parameter to the $arrFields construct.


            // allow comma or spaced emails but replace with semi colon
    if($val[1]=='email')
                {
                    $postfields[$key] = str_replace(",",";",trim($postfields[$key]));
                    $postfields[$key] = str_replace(" ",";",trim($postfields[$key]));
                }
                // basic check required fileds are completed
                if($val[4]==true && !array_key_exists($key,$postfields))
                {
                    $err[$stagekey][$key] = array($val[0],'This is a required field');
                }
                elseif(array_key_exists($key,$postfields) && $this->validateField($postfields[$key],$val[1],$val[4])==false)
                {
                    $err[$stagekey][$key] = array($val[0],'Invalid '.$val[1].' value.');
                //check values for basic field length
                }
                elseif(strlen($postfields[$key])>$feildcolset[$key][0] && $feildcolset[$key][0]!=null)
                {
                    $err[$stagekey][$key] = array($val[0],'Field max '.$feildcolset[$key][0].' characters.');
                }

        }
    }
}

HTH - i can expand on it if needed :)

2
  • could you please expand on the inner foreach loop Commented Feb 15, 2011 at 13:59
  • Sure, that'll simply loop round each posted value, and then check against the requirements. I have editred the answer with my full function, if it helps you customise it a bit :) You could add to the arrays: 'accountname' => array('Account Name','text','text','stage1',true,'<some regexp>'), You would then test $val[6] (i think) if it's set then add an if to run the regexp check Commented Feb 15, 2011 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.