我正在尝试完成一项任务,但无法使代码验证通过。问题是我需要在表单中的电子邮件文本框中检查是否存在“@”,但同时我还需要检查姓名文本框是否为空。我只能让其中一个起作用。
这是我的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Weclome to the Recipe Collection Website</title>
<script>
function validateForm()
{
var x=document.forms["myForm"]["name"].value;
if (x==null || x=="")
{
alert("Name text box must be filled out");
return false;
}
</script>
<script>
function checkEmail()
{
var y=document.forms["myForm"]["email"].value;
var atpos=y.indexOf("@");
var dotpos=y.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=y.length)
{
alert("Not a valid e-mail address");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Number of recipes previously submitted: <input type="radio" name="recipes" value="0">0<br>
<input type="radio" name="recipes" value="1-2">1-2<br>
<input type="radio" name="recipes" value="3-4">3-4<br>
<input type="radio" name="recipes" value="5+">5+<br>
Have you ever submitted recipes to another site?: <input type="radio" name="othersite" value="yes">Yes<br>
<input type="radio" name="othersite" value="no" checked>No<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
这种方法是否正确?
感谢您的帮助。