由于某种原因,javascript函数"validateEmail"不起作用。有人能告诉我我做错了什么吗?我想在表单的电子邮件部分使用onblur事件,但这似乎不起作用。我尝试过其他代码来验证电子邮件,但都不起作用。
<!DOCTYPE html>
<html>
<head>
<title> Truth or Dare </title>
<script type="text/javascript">
<!--
function validateEmail() {
var email = document.getElementById('email').value;
if( email="null" || email="")
{
window.alert("Please input a valid email address");
}
}
//-->
</script>
</head>
<body>
<tr>
<form>
First Name: <input type="text" name="firstname" maxlength="30" /> <br>
Last Name: <input type="text" name="lastname" maxlength="30"/> <br>
Email: <input type="text" id="email" /> <br>
Male <input type="radio" name="gender" value="male"/>
Female <input type="radio" name="gender" value="female"/> <br>
Date to be performed:<input type="date" name="date"/><br>
Victim: <input type="text" name="victim" maxlength="30" />
<input type="submit" onclick="validateEmail();" />
</form>
</tr>
</body>
</html>
试试这个:
email == null || email == ""
if( email="null" || email="")
缺少三重相等。此外,您应该检查null
,而不是"null"
。
<!DOCTYPE html>
<html>
<head>
<title> Truth or Dare </title>
<script type="text/javascript">
<!--
function validateEmail() {
var email = document.getElementById('email').value;
if( email===null || email==="")
{
window.alert("Please input a valid email address");
}
}
//-->
</script>
</head>
<body>
<tr>
<form>
First Name: <input type="text" name="firstname" maxlength="30" /> <br>
Last Name: <input type="text" name="lastname" maxlength="30"/> <br>
Email: <input type="text" id="email" /> <br>
Male <input type="radio" name="gender" value="male"/>
Female <input type="radio" name="gender" value="female"/> <br>
Date to be performed:<input type="date" name="date"/><br>
Victim: <input type="text" name="victim" maxlength="30" />
<input type="submit" onclick="validateEmail();" />
</form>
</tr>
</body>
</html>
试着这样做:
if (email == null || email == "")
您省略了double equals ==
,它是用于比较的相等运算符。
此外,当使用值null
时,不需要将其括在引号中。
"="分配值,"=="检查值
if (email == "null" || email == "") {
window.alert("Please input a valid email address");
}
您在比较中缺少一个=,请尝试以下操作:
if( email== "null" || email== "")
否则,您将进行分配,而不是比较