gpt4 book ai didi

javascript 表单验证不起作用并且不显示任何警报

转载 作者:行者123 更新时间:2023-11-27 23:22:07 25 4
gpt4 key购买 nike

我正在使用 JavaScript 来验证 html 中的表单,但它似乎不起作用。我非常确定我的代码是正确的。我不确定出了什么问题。它没有显示任何警报。任何人都可以知道这里出了什么问题吗?提前致谢。

<script>
function validate() {
var password2 = document.getElementById("user_password").value;
var repassword1 = document.getElementById("user_repassword").value;
var email1 = document.getElementById("user_email").value;
var atpos = email1.indexOf("@");
var dotpos = email1.lastIndexOf(".");
if (password2.lenght < 7) {
alert("Password must be at least 8 characters long.");
return false;
} else if (password2 !== repassword) {
alert("* Password Does Not Match");
return false;
} else if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
alert("Not a valid e-mail address");
return false;
} else {
return true;
}
}
</script>
<form name="submitform" method="post" action="signupsuccessfull.php" onsubmit="return validate(this)" >
<p id="error_para">* fields are required</p>
<span id="error_para">*</span> Name:<input type="text" name="name" required /><br /><br />
<span id="error_para">*</span> Username:<input type="text" name="username" required id="field_username" /><br /><br />
<span id="error_para">*</span> Password:<input type="password" name="password" id="user_password" required />
<br /><br />
<span id="error_para">*</span> Re-Enter Password:<input type="password" name="repassword" id="user_repassword" required /><br /><br />
<span id="error_para">*</span> Email:<input type="text" name="email" id="user_email" required />
<br /><br />
<span id="error_para">*</span> Gender:<input type="radio" name="gender" value="male" required />Male
&nbsp;<input type="radio" name="gender" value="female" />Female<br />
About Urself:
<textarea rows="5" cols="18" name="about" ></textarea>
<br /><br />
<input type="submit" value="Create Account" name="submit" />
&nbsp;<input type="button" value="Cancle" onClick="parent.location='../example/try2.html'"><br /><br />
</form>

最佳答案

您的代码中有错误。可能还有更多..

if (password2.lenght < 7) {   // length is the right usage
...

} else if (password2 !== repassword) { // it is repassword1 as per you defined.

使用浏览器控制台(开发人员工具)并查找并修复所有这些问题。

除非调用代码,否则函数内的 JavaScript 错误不会显示,因此其余代码仍将正常工作。

关于javascript 表单验证不起作用并且不显示任何警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35310095/

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