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

jQuery Form Validation and Manipulation code help

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??

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();

Thanks

1 Answer

Hmmmm not to sure why the first few lines of code are formatting right in above post but hopefully you get the general idea! :)