gpt4 book ai didi

javascript - 表单验证失败但仍然提交

转载 作者:行者123 更新时间:2023-11-30 10:39:38 26 4
gpt4 key购买 nike

我有以下 JS 代码:

function checkFull(id) {
var input = $('#' + id).val();
if (input == "") {
return false;
}
else {
return true;
}
}

function checkSame(id1,id2) {
var input1 = $('#' + id1).val();
var input2 = $('#' + id2).val();
if (input1 == input2) {
return true;
}
else {
return false;
}
}
function showErrors() {
if (!checkFull('username')) {
$('#userfield').show();
}
else {
$('#userfield').hide();
}

if (!checkFull('email')) {
$('#notsame').show();
}
else {
$('#notsame').hide();
}

if (!checkFull('confemail')) {
$('#notsame2').show();
}
else {
$('#notsame2').hide();
}

if (!checkFull('espncode')) {
$('#nocode').show();
}
else {
$('#nocode').hide();
}

if (notSame('email','confemail')) {
$('#notsame2').show();
$('#notsame').show();
}
else {
$('#notsame2').hide();
$('#notsame').hide();
}
}
function valform() {
showErrors();
if (checkFull('username') && checkFull('email') && checkFull('confemail') && checkSame('email','confemail') && checkFull('espncode')) {
form.submit();
alert('success');
}
else {

return false;
}
}

和下面的形式:

<form method="post" action="forms/post.asp" onsubmit="valform();return false">
<strong>Poker Username</strong> <span id="userfield" style="display:none;color:#F00">*</span><br />
<input type="text" name="username" id="username" style="width:230px;" />
<br /><br />
<div class="vert">
<strong>Email Address</strong> <span id="notsame" style="display:none;color:#F00">*</span><br />
<input type="text" name="email" id="email" style="width:230px;" />
</div>
<div class="vert2">
<strong>Confirm Email Address</strong> <span id="notsame2" style="display:none;color:#F00">*</span><br />
<input type="text" name="confemail" id="confemail" style="width:230px;" />
</div>
<div style="clear:both;"></div>
<div class="espn">
<div class="txt"><strong>Enter the ESPN special code</strong></div>
<input type="text" name="espncode" id="espncode" style="width:230px;" />
</div>
<div id="nocode" style="display:none">
You need to enter the code above
</div>
<div class="tc">
<input type="checkbox" id="agree" name="agree" /> <strong>You agree to the terms and conditions of this sweepstake. Only one entry per person per sweepstake period. Any additional entries will be disqualified.</strong>
</div>
<br />
<div class="submit">
<input type="image" src="submit_BTN.png" />
</div>
</form>

即使我将表单留空,当我按下提交时它也会通过。这是为什么?

最佳答案

您需要从 submit 处理程序中的验证函数返回值:

onsubmit="valform();return false"

应该是:

onsubmit="return valform();"

每当 valform 返回 false 时,这将阻止表单提交。

顺便说一句,valform 函数中的 form.submit() 调用毫无意义。如果函数不返回 false,无论如何都会提交。

if (/*validity checks pass*/) {
alert('success');
}
else {
return false;
}

但是,查看您的代码 with this fiddle我注意到有一个错误:

ReferenceError: Can't find variable: notSame

如果您修复函数的名称,它将起作用。您应该使用可用的开发人员工具来调试此类小错误。有适用于 Firefox 和 Chrome/Safari 的 Firebug 内置了一个网络检查器。Opera 也有 Dragonfly。

关于javascript - 表单验证失败但仍然提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879459/

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