首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >结果无需验证即可显示

结果无需验证即可显示
EN

Stack Overflow用户
提问于 2021-03-04 17:28:00
回答 1查看 26关注 0票数 0

我正在做一个测验/madlibs类的网站,所以除了验证之外,所有的东西都可以工作。每当用户输入特殊字符或将输入字段留空时,应该会出现一个警告,并且不应该允许submit按钮显示最终结果。

现在,当我输入例如"?“然后按下submit按钮,它将显示所有答案的最终结果。

我的验证做错了什么?我怎么做才能让你不能把它留空并使用特殊字符。

我用下面的代码检查验证:

代码语言:javascript
复制
<?php
    $Input1 = $Input2 = $Input3 = $Input4 = $Input5 = $Input6 = $Input7 = "";
    $Input1Err = $Input2Err = $Input3Err = $Input4Err = $Input5Err = $Input6Err = $Input7Err = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if(empty($_POST["Input1"])){
        $Input1Err = "enter something.";
      }else{
        $Input1 = test_input($_POST["Input1"]);
        if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){
          $Input1Err = "only letters and white space allowed";
        }
      }
    
    
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    } ?>

对于每个输入,我进行了如下验证:

代码语言:javascript
复制
      if(empty($_POST["Input1"])){
        $Input1Err = "enter something.";
      }else{
        $Input1 = test_input($_POST["Input1"]);
        if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){
          $Input1Err = "only letters and white space allowed";
        }
      }

下面是完整的html:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
   <title>Mad Libs</title>
   <link rel="stylesheet" type="text/css" href="Site.css">
</head>
<body>
    <h1 class="text">Quiz game</h1>
<div class="container">
    <nav>
        <ul>
            <li><a href="Quiz.php">quiz</a></li>
        </ul>
    </nav>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
        <h1> quiz</h1>
    
        <?php
            if($_SERVER["REQUEST_METHOD"] == "POST"){
                $Input1 = $_POST["Input1"];
                $Input2 = $_POST["Input2"];
                $Input3 = $_POST["Input3"];
                $Input4 = $_POST["Input4"];
                $Input5 = $_POST["Input5"];
                $Input6 = $_POST["Input6"];
                $Input7 = $_POST["Input7"];

                echo "Driving a car can be fun if you follow this $Input1. advice:
                When approaching a $Input2 on the right, always blow your $Input4 Before making a
                $Input3 turn, always stick your $Input2 out of the window.
                Every 2000 miles, have your  $Input1.inspected and your $Input5 checked.
                When approaching a school, watch out for  $Input6. 
               Above all, drive $Input6 the $Input7 you save may be your own!$Input2";
            
        ?>

        <?php }else{ ?>
        <p>Question.. <input type="text"  name="Input1" placeholder="enter here"></p> 


        <p>Question.. <input type="text"  name="Input2" placeholder="enter here"></p>

        <p>Question.. <input type="text"  name="Input3" placeholder="enter here"></p>

        <p>Question.. <input type="text"  name="Input4" placeholder="enter here"></p>

        <p>Question.. <input type="text"  name="Input5" placeholder="enter here"></p>

        <p>Question.. <input type="text"  name="Input6" placeholder="enter here"></p>

        <p>Question.. <input type="text"  name="Input7" placeholder="enter here"></p>

        <input type="submit" name="submit" value="Send">
    </form>
    <?php } ?>

    <footer>
       <p>@2021.</p>
    </footer>
</div>
 </body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2021-03-04 17:54:40

我认为你不必验证输入是否为php中带空格的文本。您可以使用html输入模式属性。例如:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
  <label for="maininput">Input</label>
  <input type="text" id="maininput" name="maininput" pattern="[A-Za-z\s]+" title="Write Here your Error"><br>
  <input type="submit">
</form>
</body>
</html>

在本例中,输入只接受字母和空格。您还可以通过将模式更改为A-Za-z0-9\s+来添加数字。另外,如果你真的想继续使用php来验证输入,我认为if(!preg_match("/[^a-z0-9 _]+$/i", $Input1)){行中的验证是错误的。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66472164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档