如何插入正则表达式以仅允许名称字段上的字母和空格并禁止" - "仅在电话字段中输入短划线?这是代码。我相信我必须插入一个elseif条件,但我很难构建整个事情。
// loop through each of our form fields
foreach ($fdata as $field => $value) {
// Now switch functionality based on field name
switch ($field) {
// name
case 'name':
if (empty($value)) {
array_push($errors, parseMessage($translations->form->error->required->$lang, array($field)));
}
break;
// email
case 'email':
if (empty($value)) {
array_push($errors, parseMessage($translations->form->error->required->$lang, array($field)));
} elseif (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
array_push($errors, $translations->form->error->email->$lang);
}
break;
// phone number
case 'phone':
if (!empty($value) && !is_numeric($value)) {
array_push($errors, parseMessage($translations->form->error->numeric->$lang, array($field)));
}
break;
// message
case 'honey':
if (!empty($value)) {
array_push($errors, $translations->form->error->honeypot->$lang);
}
break;
}
}
答案 0 :(得分:0)
除非我误解了这个问题,否则这就是你所需要的:
switch ($field) {
case 'name':
# Check if the name is empty.
if (empty($value)) {...}
# If not, check if the name consists of letters and spaces.
else if (preg_match("/^[a-z ]+$/i")) {...}
break;
case 'phone':
# Check if the phone is empty.
if (empty($value)) {...}
# If not, check if the phone does not contain a hyphen.
else if (preg_match("/^[^-]+$/")) {...}
break;
}