我试图做一个任务,但无法让代码验证。问题是我需要表单中的电子邮件文本框来检查“@”是否存在,但同样的形式我需要检查名称文本框是否为空。我只能让其中一个同时工作。
这是我的编码。
<?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>
这条路走吗?
谢谢你的帮助。