gpt4 book ai didi

JavaScript 没有验证响应

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:37 24 4
gpt4 key购买 nike

我是 javascript 的新手,我正在尝试创建一个简单的表单验证。当我点击提交按钮时,没有任何反应。我已经看了一段时间的例子,但我似乎无法弄清楚我哪里出错了。任何建议:

在这篇文章之后,我将把它全部分解并从更小的部分开始。但与此同时,我认为另一双眼睛不会伤害我,而且我很可能做错了一些可怕的事情。

HTML:

<form name="form" action="index.html" onsubmit="return construct();" method="post">
<label>Your Name:<span class="req">*</span> </label>
<input type="text" name="name" /><br />
<label>Company Name:<span class="req">*</span> </label>
<input type="text" name="companyName" /><br />
<label>Phone Number:</label>
<input type="text" name="phone" /><br />
<label>Email Address:<span class="req">*</span></label>
<input type="text" name="email" /><br />
<label>Best Time to be Contacted:</label>
<input type="text" name="TimeForContact" /><br />
<label>Availability for Presenting:</label>
<input type="text" name="aval" /><br />
<label>Message:</label>
<textarea name="message" ROWS="3" COLS="30"></textarea>
<label>First Time Presenting for AGC?:<span class="req">*</span></label>
<input type="radio" name="firstTime" value="Yes" id="yes" /><span class="small">Yes</span>
<input type="radio" name="firstTime" value="No" id="no"/><span class="small">No</span><br /><br />
<input type="submit" name="submit" value="Sign-Up" />
</form>

JavaScript:

function construct() {
var name = document.forms["form"]["name"].value;
var companyName = document.forms["form"]["companyName"].value;
var email = document.forms["forms"]["email"].value;
var phone = document.forms["forms"]["phone"].value;
var TimeForC = document.forms["forms"]["TimeForContact"].value;
var availability = document.forms["forms"]["aval"].value;

if (validateExistence(name) == false || validateExistence(companyName) == false)
return false;
if (radioCheck == false)
return false;
if (phoneValidate(phone) == false)
return false;
if (checkValidForOthers(TimeForC) == false || checkValidForOthers(availability) == false)
return false;
if (emailCheck(email) == false)
return false;
}
function validateExistence(name) {
if (name == null || name == ' ')
alert("You must enter a " + name + " to submit! Thank you."); return false;
if (name.length > 40)
alert(name + " is too long for our form, please abbreviate."); return false;
}

function phoneValidate(phone) {
if (phone.length > 12 || phone == "" || !isNaN(phone))
alert("Please enter a valid phone number."); return false;
}

function checkValidForOthers(name) {
if (name.length > 40)
alert(name + " is too long for our form, please abbreviate."); return false;
}

function messageCheck(message) {
var currentLength = name.length;
var over = 0;
over = currentLength - 200;

if (name.length > 200)
alert(name + " is too long for our form, please abbreviate. You are " + over + " characters over allowed amount"); return false;
}

function radioCheck() {
if (document.getElementById("yes").checked == false || document.getElementById("no").checked == false)
return false;
}

function emailCheck(email) {
var atpos = email.indexOf("@");
var dotpos = email.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
alert("Not a valid e-mail address");
return false;
}
}

我是否错误地调用了我的函数?老实说,我不确定我哪里出错了。
我不明白如何调试我的代码...我正在使用 chrome,但我在控制台中没有收到任何错误。有没有办法设置断点来单步执行 javascript?

我意识到我只是在那里扔了很多代码,所以在此先感谢您筛选它。

最佳答案

这里是错误的:

替换 var email = document.forms["forms"]["email"].value;

通过 var email = document.forms["form"]["email"].value;

你的js中有很多地方:

var email = document.forms["forms"]["email"].value;

var phone = document.forms["forms"]["phone"].value;

var TimeForC = document.forms["forms"]["TimeForContact"].value;

var availability = document.forms["forms"]["aval"].value;

您将 form 误输入为 forms

有没有办法设置断点来逐步执行 javascript?

是的,有一种方法可以设置断点:

enter image description here

请参阅以下链接以了解在 Chrome 调试器控制台中设置断点的方法:

LINK 1

LINK 2

关于JavaScript 没有验证响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15977078/

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