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 Build an Interactive Website Form Validation and Manipulation Checking Values

Form validation code not working correctly

Ok I have been working through the interactive website project and all been going fine until I got to the form validation. I got the code in to validate the email by seeing there is a @ present but then when it came to the required fields code something has gone wrong and now form span text is always present! I have scanned my code and cant see where I have messed up can anyone else help??

<script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript">
            var $submit = $(".submit input");
            var $required = $(".required");
            function containsBlanks(){
                var blanks = new Array();
                $required.each(function(){
                    blanks.push($(this).val() == "");
                });
                return blanks.sort().pop();
            }

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

            function requiredFilledIn(){
                if(containsBlanks() || !isValidEmail($("#email").val())) 
                    $submit.attr("disabled","disabled");
                else $submit.removeAttr("disabled");
            }

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

            $("#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");
            });
            requiredFilledIn();

        </script>

Thanks

1 Answer

Once you have created the function, you must declare a new array, then select all inputs with a class of ".required". This will then assign the selected elements to a jQuery object, then a method can be called to go over every element and carry out the required actions.

Inside this function add a value of the current element to the created array. The best way of doing sois by pushing the value onto the array (Push adds a new item to the end of an array). You can then access the value by calling .val() on $this.

I believe the following code would work.

var requiredValues = function()
{
     var myArray = new Array();
     $('.required').each(function()
     {
           myArray.push($(this).val());
     });
     return myArray;
}

Thanks for this answer Callum. Im still not sure what was wrong with the code and how/why it stopped working even after following the steps. However once I carried on with the project it started working again. I'm guessing I had something in the wrong place or maybe missing a () somewhere...who knows! Javascript baffles me! Thanks again for your help tho.