0

For some reason, the javascript function "validateEmail" isn't working. Could someone please let me know what I did wrong? I wanted to use an onblur event within the email section of the form, but that doesn't seem to be working. I have tried other codes to validate the email but none worked.

<!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>
Danielle Stewart
  • 89
  • 2
  • 3
  • 12

5 Answers5

1

Try this:

email == null || email == ""
gr3g
  • 2,866
  • 5
  • 28
  • 52
1

if( email="null" || email="") is missing triple equals. Also you should be checking for null, not "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>
Miguel Mota
  • 20,135
  • 5
  • 45
  • 64
0

Try doing it like this:

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

You left out the double equals ==, which is the equality operator for comparisons.

Also, when using the value null, you don't need to enclose it in quotes.

phantom
  • 1,457
  • 7
  • 15
0

"=" to assign value, "==" to check value

if (email == "null" || email == "") {
    window.alert("Please input a valid email address");
}
Leibale Eidelman
  • 2,772
  • 1
  • 17
  • 28
-2

You are missing one = in the comparison, try this:

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

Otherwise you are assigning instead of comparing

carito
  • 132
  • 3