我正在创建一个基于表单值的正则表达式数组,并使用在错误的用户输入上失败的函数。每次我运行网站时都会收到以下错误:
警告: eregi()[function.eregi]:REG_EMPTY
我不知道出了什么问题。请查看我的代码和帮助。谢谢!
$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' => 'John@myemail.com'
);
$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;
}
答案 0 :(得分:2)
我的方法略有不同。我有一个像这样的数组 - 我最终传给Smarty来构建我的 - 但是在提交时,这个数组被传递,它将$ _POST形成一个循环的函数并进行必要的验证(基于这里的第二个参数) );
$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)
)
);
我做一些自定义的东西,但基本上循环它是:
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 - 我可以根据需要进行扩展:)