Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Building an interactive site: form validation

My validation isn't working for some reason... the submit button isn't disabled AND the email validation is failing, here is my code, this is only a snippet, but I did include Jquery.

        <script type="text/javascript">
      var $submit = $(".submit input");
      var $required = $(".required");
      function requiredFilledIn(){
        if(containsBlanks()) $submit.attr("disabled", "disabled");
        else $submit.removeAttr("disabled");
      }

      function containsBlanks(){
        var blanks = new Array();
        required.each(function(){
          blanks.push($(this).val() == "");
        });
        return blanks.sort().pop() != -1;
      }

      function isValidEmail(email){
        return email.indexOf("@") != -1;
      }

      $("#form span").hide();
      $("input, textarea").focus(function(){
        $(this).next().fadeIn("slow");
      }).blur(function(){
        $(this).next().fadeOut("slow");
      }).keyup(function(){
        //Check all required fields.
        requireFilledIn();
      });

      $("#email").keyup(function(){
        //Check for a valid email
        if(isValidEmail($(this).val())) $(this).next().removeClass("error").addClass("valid");
        else $(this).next().removeClass("valid").addClass("error");
      });
      requireFilledIn();
        </script>

Please include your HTML code as well.

good call.

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Smells Like Bakin' Cupcake Company</title>
    <link rel="stylesheet" href="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/css/normalize.css" type="text/css" media="screen">
    <link rel="stylesheet" href="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/css/grid.css" type="text/css" media="screen">
    <link rel="stylesheet" href="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/css/style.css" type="text/css" media="screen">
    <link href='http://fonts.googleapis.com/css?family=Nunito:400,300,700' rel='stylesheet' type='text/css'>
    <script src="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/external/matchmedia.js"></script>
    <script src="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/picturefill.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale = 1.0, user-scalable = no">
</head>

<body>

    <div class="container clearfix">
        <div id="logo" class="grid_4">
            <object data="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/img/logo.svg" type="image/svg+xml" class="logo">
                <a href="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/logo.svg" >
                    <!--[if lte IE 8 ]-->
                    <img src="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/img/logo.gif" alt="Smells Like Bakin">
                    <!--![endif]-->
                </a>
            </object>

        </div>
        <div id="nav" class="grid_8 omega" >
            <ul>
                <li class="about"><a href="#">About</a></li>
                <li class="pricing"><a href="#">Cupcakes & Prices</a></li>
                <li class="locations"><a href="#">Locations</a></li>
                <li class="contact"><a href="#">Contact Us</a></li>
            </ul>
        </div>

        <div class="grid_12">
            <h1 class="hero">We have a wide variety of delicious desserts that are sure to take you by surprise!</h1>
        </div>


        <div id="form" class="grid_12 omega">
            <h2>Contact Smells Like Bakin'</h2>
            <form action="contact.php" method="post">

                <p>
                    <label for="name">Name:</label>
                    <input name="name" id="name" type="text" class="required">
                    <span>Please enter your name</span>
                </p>

                <p>
                    <label for="email">Email:</label>
                    <input name="email" id="email" type="text" class="required"> 
                    <span>Please enter a valid email address</span>
                </p>

                <p>
                    <label for="subject">Subject:</label>
                    <input name="subject" id="subject" type="text"> 
                    <span>Please enter your subject</span>
                </p>

                <p>
                    <label for="message">Message</label>
                    <textarea name="message" id="message" class="required"></textarea> 
                    <span>Please enter your message</span>
                </p>
                <p class="submit">
                    <input type="submit" value="Submit" class="btn-submit">
                </p>
            </form>


        </div>



        <div id="footer" class="grid_12">
            <div id="about" class="grid_7">
                <h2>Inside the Kitchen</h2>
                <p>Smells Like Bakin’ started out in the garage of the husband wife duo Allison &amp; Joseph. Allison is the baker, and Joseph found a way for them to make a business out of her tasty treats. Flash forward to today and they have a successful store front, catering business and cupcake truck. </p>
                <p><a href="#" class="btn-small">Read More</a></p>
            </div>


            <div id="contact" class="grid_5 omega">
                <h2>Get Bakin’ with Us</h2>
                <p>Call us: <span class="contact">1-800-CUP-CAKE</span><br>
                    Email us: <a href="#">bakeon@smellslikebakin.com</a></p>

                    <p>We announce all of our new flavors first through Facebook &amp; Twitter, and even take requests!</p>     

                    <object data="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/img/facebook.svg" type="image/svg+xml">
                        <a href="facebook.svg">
                            <!--[if lte IE 8 ]-->
                            <img src="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/img/facebook.gif" alt="Facebook">
                            <!--![endif]-->
                        </a>
                    </object>

                    <object data="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/img/twitter.svg" type="image/svg+xml">
                        <a href="twitter.svg">
                            <!--[if lte IE 8 ]-->
                            <img src="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/img/twitter.gif" alt="Twitter">
                            <!--![endif]-->
                        </a>
                    </object>
                </div>

                <div id="copyright" class="grid_12">
                    <p>© 2012 Smells Like Bakin' Cupcake Company. All Rights Reserved.</p>
                </div>

            </div>

        </div>

        <script type="text/javascript" src="http://treehouse-code-samples.s3.amazonaws.com/WWIsland3/codepen/js/jquery.js"></script>
        <script type="text/javascript">
      var $submit = $(".submit input");
      var $required = $(".required");
      function requiredFilledIn(){
        if(containsBlanks()) $submit.attr("disabled", "disabled");
        else $submit.removeAttr("disabled");
      }

      function containsBlanks(){
        var blanks = new Array();
        required.each(function(){
          blanks.push($(this).val() == "");
        });
        return blanks.sort().pop() != -1;
      }

      function isValidEmail(email){
        return email.indexOf("@") != -1;
      }

      $("#form span").hide();
      $("input, textarea").focus(function(){
        $(this).next().fadeIn("slow");
      }).blur(function(){
        $(this).next().fadeOut("slow");
      }).keyup(function(){
        //Check all required fields.
        requireFilledIn();
      });

      $("#email").keyup(function(){
        //Check for a valid email
        if(isValidEmail($(this).val())) $(this).next().removeClass("error").addClass("valid");
        else $(this).next().removeClass("valid").addClass("error");
      });
      requireFilledIn();
        </script>
    </body>
    </html>

2 Answers

I made a code-pen here to play with it: http://codepen.io/anon/pen/Cweyu

There were a few typos in the JavaScript, one at the top where you define the function requiredFilledIn() should be called requireFilledIn().

Apart from that the code you have so far checks out. What you are missing is acting on failed validation. You have the functions in place to check for it, but you don't act on the result. There is no IF statement that triggers if the function returns false to show the error message.

Also, you are declaring the submit and required variables with $-signs. Remove those (there are some more occurrences in the code, and the submit button will indeed turn gray. :)

I was able to get the code working now. Check the code in the link above and compare it to what you have.

Thank you the help! Could you elaborate on the missing if statement?

actually I think I have solved it, thank you for the help, been looking at the code too damn long.