1

我想通过最小和最大长度验证表单字段。

我已经在验证它们是否为空或电子邮件地址是否无效。我想为每个字段添加额外的长度要求(最小长度和最大长度)。什么是最好的做法?

摘自我的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
4

2 回答 2

1

在 HTML5 中,您可以使用pattern属性,如下所示:

<input pattern=".{5,25}" title="Input 5 to 25 characters">

当然,这并不安全。您还需要在服务器端验证输入。

您可以使用strlen()获取输入值的长度,然后使用if条件语句检查它们是否在预定义的长度范围内。

$length = strlen(trim($_POST['name']));
if ( $length >= 5 && $length <= 25) {
    # code...
}

将其集成到您当前的代码中,它将类似于:

if (empty($name)) {
    $errors['name'] = MIA_NAME;    // Error message for missing name
}

if (strlen(trim($_POST['name'])) >= 5) {
    # code ...
} elseif (strlen(trim($_POST['name'])) <= 25) {
    # code ...
}

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
}

希望这可以帮助!

于 2013-08-28T09:23:20.620 回答
0
$name= clean_input($_POST['name']); //A custom function to make sure it's a clean string
if(strlen($name) > 100)
{
  //Something weird
}
else
{
  //We're good
}
于 2013-08-28T09:16:17.917 回答