我想通过最小和最大长度验证表单字段。
我已经在验证它们是否为空或电子邮件地址是否无效。我想为每个字段添加额外的长度要求(最小长度和最大长度)。什么是最好的做法?
摘自我的process.php
文件:
require_once('includes/phpmailer-config.php');
require_once('includes/phpmailer/class.phpmailer.php');
// other stuff here
// Validate each form field
if (empty($name)) {
$errors['name'] = MIA_NAME; // Error message for missing name
}
if (empty($email)) {
$errors['email'] = MIA_ADDR; // Error message for missing email address
}
elseif (!(filter_var($email, FILTER_VALIDATE_EMAIL))) {
$errors['email'] = INV_ADDR; // Error message for invalid email address
}
if (empty($message)) {
$errors['message'] = MIA_MESG; // Error message for missing message
}
摘自我的phpmailer-config.php
文件:
// other stuff here
define ('MIA_NAME', 'A Full Name is Required.'); // Error for missing name
define ('LG_NAME', {5,50}'); // Min. length: 5, Max. length: 50
define ('TS_NAME', 'Name is too Short.'); // Error for name too short
define ('TL_NAME', 'Name is too Long.'); // Error for name too long
define ('MIA_ADDR', 'An Email Address is Required.'); // Error for missing email address
define ('LG_ADDR', {8,50}'); // Min. length: 8, Max. length: 50
define ('TS_ADDR', 'Email Address is too Short.'); // Error for email too short
define ('TL_ADDR', 'Email Address is too Long.'); // Error for email too long
define ('INV_ADDR', 'We need a valid email address.'); // Error for invalid email address
define ('MIA_MESG', 'A Message is Required.'); // Error message for missing message
define ('LG_MESG', {20,7500}'); // Min. length: 20, Max. length: 7500
define ('TS_MESG', 'Message is too Short.'); // Error for message too short
define ('TL_MESG', 'Message is too Long.'); // Error for message too long
编辑:感谢Amal的回答,这是我最终使用的:
摘自phpmailer-config.php
:
define ('MIA_NAME', 'A Full Name is Required.'); // Error for missing name
define ('MIN_NAME', 5); // Min. length for name
define ('MAX_NAME', 50); // Max. length for name
define ('TS_NAME', 'Name is too Short.'); // Error for name too short
define ('TL_NAME', 'Name is too Long.'); // Error for name too long
define ('MIA_ADDR', 'An Email Address is Required.'); // Error for missing email address
define ('MIN_ADDR', 8); // Min. length for email address
define ('MAX_ADDR', 50); // Max. length for email address
define ('TS_ADDR', 'Email Address is too Short.'); // Error for email too short
define ('TL_ADDR', 'Email Address is too Long.'); // Error for email too long
define ('INV_ADDR', 'We need a valid email address.'); // Error for invalid email address
define ('MIA_MESG', 'A Message is Required.'); // Error message for missing message
define ('MIN_MESG', 20); // Min. length for message
define ('MAX_MESG', 7500); // Max. length for message
define ('TS_MESG', 'Message is too Short.'); // Error for message too short
define ('TL_MESG', 'Message is too Long.'); // Error for message too long
摘自process.php
:
// Validate each form field
if (empty($name)) {
$errors['name'] = MIA_NAME; } // Missing name
elseif (strlen(trim($_POST['name'])) < MIN_NAME ) {
$errors['name'] = TS_NAME; } // Name too short
elseif (strlen(trim($_POST['name'])) > MAX_NAME ) {
$errors['name'] = TL_NAME; } // Name too long
if (empty($email)) {
$errors['email'] = MIA_ADDR; } // Missing email address
elseif (strlen(trim($_POST['email'])) < MIN_ADDR ) {
$errors['email'] = TS_ADDR; } // Email too short
elseif (strlen(trim($_POST['email'])) > MAX_ADDR ) {
$errors['email'] = TL_ADDR; } // Email too long
elseif (!(filter_var($email, FILTER_VALIDATE_EMAIL))) {
$errors['email'] = INV_ADDR; } // Invalid email address
if (empty($message)) {
$errors['message'] = MIA_MESG; } // Missing message
elseif (strlen(trim($_POST['message'])) < MIN_MESG ) {
$errors['message'] = TS_MESG; } // Message too short
elseif (strlen(trim($_POST['message'])) > MAX_MESG ) {
$errors['message'] = TL_MESG; } // Message too long