我正在尝试使用 javascript 验证我的表单,但它似乎不起作用。
谁能看到我的代码中的错误在哪里?
这是我的 HTML 代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css/stylesheet.css"/>
<script src="js/validation.js"></script>
<title>Form</title>
</head>
<body>
<form name="myForm" method="post" onsubmit="return validateForm()">
<fieldset>
<legend> Please fill out this Form</legend>
<p>
<label class="field" for"name"> First name:</label>
<input type="text" name="fname" class="textbox-300"/>
</p>
<p>
<label class="field" for"name">Last name:</label>
<input type="text" name="lname" class="textbox-300"/>
</p>
<p>
<label class="field" for"name">Email:</label>
<input type="text" name="email" class="textbox-300"/>
</p>
<p>
<label class="field" for"name">Phone:</label>
<input type="text" name="phonenumber" class="textbox-300"/>
</p>
<p>
<label class="field" for"name">Adress:</label>
<input type="text" name="address" class="textbox-300"/>
</p>
<p>
<input type="submit" value="submit">
</p>
</fieldset>
</form>
</body>
</html>
我的 Java Script 代码来验证我的 HTML
function validateForm() {
var x = document.forms['myForm']['fname'].value;
var y = document.forms['myForm']['lname'].value;
var i = document.forms['myForm']['email'].value;
var j = document.forms['myForm']['phone'].value;
var address = document.forms['myForm']['address'].value;
var atpos = i.indexOf("@");
var dotpos = i.lastIndexOf(".");
// ______________________________________________
if (x == null || x == "") {
alert("First Name must be Entered");
return false;
}
//___________________________________________
if (y == null || y == "") {
alert("Last Name must be Entered");
return false;
}
//___________________________________________
if (parseInt(j) != j) {
alert("Please enter a correct phone number");
return false;
}
//___________________________________________
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= i.length) {
alert("Not a valid e-mail address");
return false;
}
//___________________________________________
if (address == null || address == "") {
alert("You most enter your Address");
return false;
}
}
我一遍又一遍,我找不到错误。