当我按下提交按钮时,错误不显示。我也使用过 Internet Explorer 和 Google Chrome。
我正在尝试为我的 MySQL 数据库创建一个表单验证页面。
也许我的警报代码是假的?
这是我的代码:
<html>
<head>
<title>Validare</title>
<script language="JavaScript">
function ver() {
if (document.form1.name.value == "") {
alert("Te rog introdu numele");
return false;
}
If(document.form1.email.value == "") {
alert("Te rog introdu e-mail-ul")
return false;
}
if (document.form1.age.value == "") {
alert("Te rog introdu Varsta");
document.form1.age.focus();
return false;
}
if (document.form1.age.value < 18 || document.form1.age.value > 60) {
alert("Te rog introdu adresa cuprinsa in intevalul[18,60] ");
document.form1.age.focus();
return false;
}
If(document.form1.gender[0].checked == false && document.form1.gender[1].checked == false) {
alert("Te rog alege Sex-ul");
document, form1.age.focus();
return flase;
}
If(docuemnt.form1.limba1.checked == false && document.form1.limba2.checked == false && document.form1.limba3.checked == false) {
alert("Te rog selecteaza cel putin o limba");
return false;
}
if (document.form1.country.value == "") {
alert("Te rog introdu Tara");
document.form1.country.focus();
return false;
}
If(document.form1.myadress.value == "") {
alert("Te rog introdu Adresa");
document.form1.myadress.focus();
return false;
}
if (document.form1.u_name.value == "") {
alert("Te rog introdu Username");
document.form1.u_name.focus();
return false;
}
If(document.form1.pass.value == "") {
alert("Te rog introdu Password")
document.form1.pass.focus();
return false;
}
If(document.form1.pass.value.length < 6) {
alert("Te rog introdu Password mai lunga ca 6");
document.form1.pass.focus();
return false;
}
If(document.form1.r_pass.value == "") {
alert("Te rog introdu din nou parola");
document.form1.r_pass.focus();
terun false;
}
If((document.form1.pass.value) != (document.form1.r_pass.value)) {
alert("Password-ul nu se potriveste");
document.form1.r_pass.value = "";
document.form1.r_pass.focus();
return false;
}
}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table border="2" align="center" cellpadding="7" cellspacin="7">
<tr>
<td><strong>Nume</strong>
</td>
<td>
<input type="text" name="name" </td>
</tr>
<tr>
<td><strong>E-Mail</strong>
</td>
<td>
<input type="text" name="email">
</td>
</tr>
<tr>
<td><strong>Varsta</strong>
</td>
<td>
<input type="text" name="age" size="2">
</td>
</tr>
<tr>
<td><strong>Sex</strong>
</td>
<td>
<input type="radio" name="gender" value="Male">Masculin
<input type="radio" name="gender" value="Male">Feminin</td>
</tr>
<tr>
<td><strong>Limba</strong>
</td>
<td>
<input type="checkbox" name="limba1" value="Romana">Romana
<input type="checkbox" name="limba2" value="Engleza">Engleza
<input type="checkbox" name="limba3" value="Germana">Germana</td>
</tr>
<tr>
<td><strong>Tara</strong>
</td>
<td>
<select name="country">
<option value="selected">-Selecteaza-
<option value="Germ">Germania
<option value="roman">Romania
<option value="ungar">Ungaria</select>
</td>
</tr>
<tr>
<td><strong>Adresa</strong>
</td>
<td>
<textarea rows="5" cols="20" name="myadress"></textarea>
</td>
</tr>
<tr>
<td><strong>Username</strong>
</td>
<td>
<input type="text" name="u_name">
</tr>
</td>
<tr>
<td><strong>Password</strong>
</td>
<td>
<input type="password" name="pass">
</td>
</tr>
<tr>
<td><strong>Re-Type</strong>
</td>
<td>
<input type="password" name="r_pass">
</td>
</tr>
<tr align="center">
<td>
<input type="submit" value="submit" onClick="return ver(this);">
</td>
<td>
<input type="reset">
</td>
</tr>
</table>
</form>
</body>
</html>
最佳答案
从提交中删除 onclick 处理程序,并在表单元素上放置一个 sumbit 处理程序。
<form method="post" action="" name="form1" onsubmit="return ver(this);">
此外,您没有定义操作,因此不会向服务器端页面发布任何内容。其次这里有一个错别字
If(document.form1.r_pass.value == "") {
alert("Te rog introdu din nou parola");
document.form1.r_pass.focus();
terun false; //SHOULD BE return false;
}
If(document.form1.gender[0].checked == false && document.form1.gender[1].checked == false) {
alert("Te rog alege Sex-ul");
document, form1.age.focus();
return flase; //SHOULD be return false;
}
我认为还有更多的拼写错误和其他编码错误。在浏览器中使用键盘键 F12,这将打开控制台(开发工具),为您提供有关错误的信息。
关于javascript - 验证 html-javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28657889/