Results 1 to 4 of 4

Thread: basic JavaScript alert not working

  1. #1
    Join Date
    Dec 2012

    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?
    <!DOCTYPE html>
    <html lang="en">
    <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
    // -->
    <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">
    	<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>
    <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">
    <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 &copy; 2013 Fish Creek Animal Hospital</div>
    <a href="mailto:magda@patel.com">Magda@Patel.com</a></div></div></div>

  2. #2
    Join Date
    Jan 2010
    Hi bud

    You are comparing your email value to a space, if you remove the space your code should work

    if (document.forms[0].myEmail.value = = "" )

  3. #3
    Join Date
    Jan 2004
    chertsey, a small town s.w. of london, england.
    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...
    if (document.forms[0].myEmail.value == "" ) {
    ...or to save a few bytes...
    if(document.forms[0].myEmail.value=="" ){

  4. #4
    Join Date
    Dec 2012
    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
HTML5 Development Center