我正在使用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
<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" />
<input type="button" value="Cancle" onClick="parent.location='../example/try2.html'"><br /><br />
</form>
答案 0 :(得分:1)
您的代码中存在错误。可能会有更多..
if (password2.lenght < 7) { // length is the right usage
...
} else if (password2 !== repassword) { // it is repassword1 as per you defined.
使用浏览器控制台(开发人员工具)找到并解决所有这些问题。
除非调用代码,否则函数内部的javascript错误不会显示,因此其余代码仍然有效。
答案 1 :(得分:1)
有2个拼写错误。
应为password2.length
它也应该是repassword1
,而不仅仅是repassword
。
答案 2 :(得分:0)
要修复,请更改行
if (password2.lenght < 7) {
到
if (password2.length < 7) {
将repassword
更改为repassword1
答案 3 :(得分:0)
我使用下面的代码
来完成这项工作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(".");
var emilPat = /( *?[0-9a-zA-Z] *?){8,}/;
var m;
m = emilPat.exec(password2)
if(m== null){
alert("Password must be at least 8 characters long.");
return false;
}
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;
}
}
答案 4 :(得分:-1)
像这样使用:
<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 != repassword1) {
alert("* Password Does Not Match");
return false;
} else if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email1.length) {
alert("Not a valid e-mail address");
return false;
} else {
return true;
}
}
</script>