basic JavaScript alert not working
ok, I am working on an assignment (yes, school work) we are just being introduced to javascript. I have a contact page that I am trying to create an alert if the user tries to submit form without entering an email address. but it is not working. I have been staring at this for over an hour and can not find the problem. I am an online student and my teacher has not responded to my emails and this is due tonight. Can someone take a look and let me know if you can see my problem?
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Contact</title>
<meta charset="utf-8">
<link rel="stylesheet" href="fishcreek.css">
<script type="text/javascript">
<!--
function validateForm() {
if (document.forms[0].myEmail.value = = " " ) {
alert("Please enter an email address.");
return false;
} //end if
return true;
} // end function validateForm
// -->
</script>
</head>
<body>
<div id="wrapper">
<h1 id="header"><img src="fishcreeklogo.gif" height="77" width="600" alt="Fish Creek Animal Hospital"></h1>
<div class="nav" id="leftcolumn">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="services.html">Services</a><li>
<li><a href="askvet.html">Ask the Vet</a><li>
<li><a href="contact.html">Contact</a><li>
</ul></div>
<br>
<div id="rightcolumn">
<h2>Contact Fish Creek</h2>
<form method="post" action="http://webdevbasics.net/scripts/fishcreek.php"
onsubmit="return validateForm();">
<label for="myName">Name: </label><input type="text" name="myName" id="myName">
<label for="myEmail">E-mail:</label> <input type="text" name="myEmail" id="myEmail">
<label for="myComments">Comments: </label>
<textarea name="myComments" id="MyComments" rows="2" cols="20"></textarea>
<input id="mySubmit" type="submit" value="Send Now">
<br></form>
<div ID= "footer"><div class="nav">
<a href="index.html">Home</a>
<a href="services.html">Services</a>
<a href="askvet.html">Ask the Vet</a>
<a href="contact.html">Contact</a><br>
Copyright © 2013 Fish Creek Animal Hospital</div>
<a href="mailto:magda@patel.com">Magda@Patel.com</a></div></div></div>
</body>
</html>
Hi bud
You are comparing your email value to a space, if you remove the space your code should work
Code:
if (document.forms[0].myEmail.value = = "" )
Hi there bearsnakes,
and a warm welcome to these forums.
Further to Vince616's comment, you also have a syntax error in your "equality operator ".
There must not be a space between the "= " symbols.
So use at least...
Code:
if (document.forms[0].myEmail.value == "" ) {
...or to save a few bytes...
Code:
if(document.forms[0].myEmail.value=="" ){
coothead
thanks so very much. works great now. usually I am missing some little think like a div tag. guess I am not very good at looking. I will get better.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks