Reputation: 930
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;
}
Upvotes: 1
Views: 798
Reputation: 8616
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 :)
Upvotes: 2