【问题标题】:How can I prevent blank HTML form submissions to go through?如何防止空白 HTML 表单提交通过?
【发布时间】:2017-01-23 13:10:26
【问题描述】:

我正在使用 Formspree(这很棒)除了:我已将所有字段设置为必填,当我手动尝试时,如果没有正确填写所有内容,我无法提交表单,但是我仍然收到一堆空白表格。有没有人经历过/解决过这个问题?

谢谢!

<form action="https://formspree.io/XXXXXXXX" method="post">
<input name="_gotcha" style="display: none" type="text">

<input class="form-control input-lg" name="firstname" placeholder="First Name" required type="text">

<input class="form-control input-lg" name="lastname" placeholder="Last name" required type="text">

<input class="form-control input-lg" name="phone" placeholder="Phone number" required type="text">

<select required name="country" class="form-control input-lg" style="border-radius:none; -webkit-appearance: none; padding-top:0px;">
    <option value="" disabled selected>Please select your country </option>
    <option value="Afganistan">Afghanistan</option>
    <option value="Albania">Albania</option>
    <option value="other">Not in the List</option>
    <!-- etc -->
</select>

<input type="hidden" name="referrer" id="referrer_field" />
<input type="hidden" name="url" id="url_field" />

<button id="demo" type="submit">GO</button>

【问题讨论】:

  • 请分享您的代码。没有任何代码就无法工作
  • 对不起。刚刚添加了上面的代码sn-p
  • 您正在使用哪个服务器端框架。您可以添加一些服务器端代码,以便获取字段的空值
  • 不幸的是,我无法访问服务器端。这是我们用于快速迭代的静态引导登录页面。
  • required 很容易被绕过,通常在你的 PHP 中添加条件可以解决这个问题,例如 if (!empty($_POST['name_of_field']))

标签: html forms form-submit


【解决方案1】:

一个简单的解决方案是这样的。 您可以使用 JQuery 来做到这一点

//  Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {

    // Get the Login Name value and trim it
    var name = $.trim($('#log').val());

    // Check if empty of not
    if (name === '') {
        alert('Text-field is empty. You cannot leave is blank!');
        return false;
    }
});
.text-label {
    color: #cdcdcd;
    font-weight: bold;
}
#pwd{
  margin:10px 18px;
  }

#logBtn{
  margin:10px 90px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<form action="login.php" method="post">
    <label>Login Name:</label>
    <input type="text" name="email" id="log" />
    <br/>
    <label>Password:</label>
    <input type="password" name="password" id="pwd" />
    <br/>
    <input type="submit" id="logBtn" name="submit" value="Login" />
</form>

【讨论】:

  • 谢谢,马上试试!
【解决方案2】:

这是您代码的另一种解决方案,因为即使有人在姓名或电话字段中输入“spaces 或将其留空”,它也可以工作。

检查小提琴..

//  Bind the event handler to the "submit" JavaScript event
$('form').submit(function () {

    // Get the Login Name value and trim it
    var fname = $.trim($('#fname').val());
    var lname = $.trim($('#lname').val());
    var phon =$.trim($('#phon').val());

    // Check if empty of not
    if (fname === '') {
        alert('First name is empty.');
        return false;
    }
    else if (lname === '') {
        alert('Last Name is empty.');
        return false;
    }
    else if (phon === '') {
        alert('Phone is empty.');
        return false;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<form action="https://formspree.io/XXXXXXXX" method="post">
<input name="_gotcha" style="display: none" type="text">

<input class="form-control input-lg" name="firstname" placeholder="First Name" id="fname" required type="text">

<input class="form-control input-lg" name="lastname" placeholder="Last name" id="lname" required type="text">

<input class="form-control input-lg" name="phone" placeholder="Phone number" id="phon" required type="text">

<select name="country" class="form-control input-lg" style="border-radius:none; -webkit-appearance: none; padding-top:0px;">
    <option value="" disabled selected>Please select your country </option>
    <option value="Afganistan">Afghanistan</option>
    <option value="Albania">Albania</option>
    <option value="other">Not in the List</option>
    <!-- etc -->
</select>

<input type="hidden" name="referrer" id="referrer_field" />
<input type="hidden" name="url" id="url_field" />

<button id="demo" type="submit">GO</button>

如果有人在输入框中输入空格,则此 Jquery 代码将起作用并要求他在字段中填写正确的文本。

【讨论】:

  • 你可以检查一下,如果你不填写任何单词,那么required属性会要求填写。但是如果有人在这些填充中输入space 怎么办,那么我的 Jquery 代码就可以工作了。
【解决方案3】:

试试 W3Schools 的例子 http://www.w3schools.com/tags/att_input_required.asp

<input class="form-control input-lg" name="firstname" placeholder="First Name" required >

没有等价或任何东西

我认为需要的标签是

required="真/假"

【讨论】:

    猜你喜欢
    • 2016-03-23
    • 2021-01-30
    • 1970-01-01
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    相关资源
    最近更新 更多