PHP联系页面 - 如何为输入字段指定最小字母

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (28)

我在我的实时网站上有一个contact.php页面,但即使访问者在名称,电子邮件或消息字段中输入了不完整的文本,表单也会将其视为正确并显示成功消息。

如何指定名称之类的内容必须至少为6个字母,电子邮件必须包含@符号和正确的.com,.org等结尾; 消息必须至少20个字母。

PHP SCRIPT

<?php

if ($_POST['submit']) {

  if (!$_POST['firstname']) {
    $error="<br/>- Please enter your first name";
  }

  if (!$_POST['lastname']) {
    $error="<br/>- Please enter your last name";
  }

  if (!$_POST['email']) {
    $error.="<br/>- Please enter your email";
  }
  if (!$_POST['message']) {
    $error.="<br/>- Please enter a message";
  }
  if (!$_POST['check']) {
    $error.="<br/>- Please confirm you are human";
  }

  if ($error) {
    $result='<div class="alert alert-danger" role="alert">
    <strong>Whoops, there is an error</strong>. Please correct the following: '.$error.'</div>';
  } else {
    mail("myname@mywebsite.com", 
   "Contact message", 
    "Name: ".$_POST['name']."
    Email: ".$_POST['name']."
    Message: ".$_POST['message']);

    {
    $result='<div class="alert alert-success" role="alert">Thank you, I\'ll be in touch shortly. </div>';
    }
  }
}
?>

#HTML CODE#

  <form method="post" role="form">
            <div class="row">
            <div class="col-md-3"></div>
                  <div class="contact-icon">
                      <i class="fas fa-user"></i>
                  </div>

                  <div class="form-inline">
                    <div class="col-md-9 form-group">
                          <label for="name">your name</label>
                          <span class="fieldSpan">
                          <input type="text" 
                          name="name" 
                          id="input-field-name"
                          class="form-control" 
                          placeholder="your name" 
                          value="<?php echo $_POST['name']; ?>"> 
                    </div>
                </div>
            </div>

        <div class="row">
        <div class="col-md-3"></div>
              <div class="contact-icon">
                <i class="fas fa-envelope"></i>
              </div>

              <div class="form-inline">
              <div class="col-md-9 form-group">
                      <label for="email">your email address</label>
                      <span class="fieldSpan">
                      <input type="email" 
                      name="email" 
                      id="input-field-email"
                      class="form-control" 
                      placeholder="yourname@website.com" 
                      value="<?php echo $_POST['email']; ?>">
                  </div>
                </div>
         </div>

        <div class="row">
        <div class="col-md-3"></div>
              <div class="contact-icon">
                  <i class="fas fa-pencil-alt"></i>
              </div>

              <div class="form-inline">
                 <div class="col-md-9 form-group">
                        <label for="message">your message</label>
                        <span class="fieldSpan vertical-align:top">
                        <textarea name="message" 
                        rows="5" 
                        id="input-field-message"
                        class="form-control" 
                        placeholder="your message">
                        <?php echo $_POST['message']; ?></textarea>
                        <br>
                        <br>
                    </div>
                    </div>
              </div>
        </div>


              <div class="col-md-12">
                 <div class="checkbox">
                    <label>
                        <input type="checkbox" 
                        name="check"> 
                        Check this box if you're human.
                      </label>
                  </div>
              </div>
        </div>

      <div class="col-md-3"></div>
      <div class="row">
          <div class="col-md-9">
              <input type="submit" 
              name="submit" 
              class="btn btn-secondary" 
              value="send message"/>
            </div> 
        </div>

     </form>

     </div>
      </div>
    </div>
  </section>


 <!-- ----------- SCRIPTS------------ -->

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
提问于
用户回答回答于
用户回答回答于

这有你想要的答案。这是一个称为输入验证的概念,每种语言都有自己的处理方式。

以下是该链接的摘录,用于验证一些所需字段。

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }
  }

  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
      $websiteErr = "Invalid URL"; 
    }
  }

  if (empty($_POST["comment"])) {
    $comment = "";
  } else {
    $comment = test_input($_POST["comment"]);
  }

  if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}
?>

在确保字段不为空之后,您可以通过检查在if语句中查看name字段的长度是否大于或等于6来控制所需的验证类型。

扫码关注云+社区

领取腾讯云代金券