如何防止空白HTML表单提交通过?

时间:2016-09-15 13:27:06

标签: html forms form-submit

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

谢谢!

## 1
a = tf.constant(1)
b = tf.constant(1)
x = tf.add(a, b)
with tf.Session() as sess:
    x.eval()

## 2
a = 1
b = 1
x = tf.add(a, b)
with tf.Session() as sess:
    x.eval()

## 3
a = tf.constant(1)
b = tf.constant(1)
x = a + b
with tf.Session() as sess:
    x.eval()

## 4
a = 1
b = tf.constant(1)
x = a + b
with tf.Session() as sess:
    x.eval()

3 个答案:

答案 0 :(得分: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>
&#13;
&#13;
&#13;

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

答案 1 :(得分: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 :(得分:1)

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

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

没有等于或任何东西

我认为所需的标签是

需要= “真/假”