gpt4 book ai didi

javascript - JS在允许提交表单之前输入正确的短语

转载 作者:行者123 更新时间:2023-12-04 08:57:57 25 4
gpt4 key购买 nike

我正在尝试让我们的联系页面仅在输入正确的短语后才允许提交。
我不知道如何尝试警报功能。它会在输入错误短语时发出警报,但仍会提交表单。基本上我试图阻止我们的表单自动提交。
相关代码:

  **HTML:**

<label for="phrase" class="p">Please enter "security" below</label><br>
<input type="text" id="phrase" name="phrase" required></type><br>

<input type="submit"; onclick="no_spam()"; value="Submit"></type>

**JS:**

function no_spam() {
var spam = document.getElementById("phrase").value;
if(spam !== "security")
{alert("Enter correct phrase")}
}

最佳答案

你不需要任何 JavaScript;它可以使用 pattern 完全在 HTML5 中完成。属性。

<form>
<label for="phrase" class="p">Please enter "security" below</label><br>
<input type="text" id="phrase" name="phrase" required pattern="^security$" title="Enter correct phrase"></type><br>
<input type="submit" value="Submit"></type>
</form>

关于javascript - JS在允许提交表单之前输入正确的短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63710673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com