Javascript电子邮件onclick验证不起作用

时间:2014-11-03 22:41:33

标签: javascript html html5 web web-deployment

由于某种原因,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>

5 个答案:

答案 0 :(得分:1)

试试这个:

email == null || email == ""

答案 1 :(得分:1)

if( email="null" || email="")缺少三等号。此外,您应该检查null,而不是"null"

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

尝试这样做:

if (email == null || email == "")

你遗漏了双等于==,这是比较的相等运算符。

此外,使用值null时,您不需要将其括在引号中。

答案 3 :(得分:0)

“=”分配值, “==”检查值

if (email == "null" || email == "") {
    window.alert("Please input a valid email address");
}

答案 4 :(得分:-2)

你在比较中缺少一个=,试试这个:

if( email== "null" || email== "")

否则您将分配而不是比较