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

I'm having trouble getting form validation to work properly. Please help.

I'm having trouble getting the form to validate properly. I'd like it to iterate and check for user input to have a value that that isn't what their placeholder is or is not an empty string.

<form action="#" method="post" onsubmit = "formValidate()"> <input id="home-worth-name" class ="validateMe" value="Name*" name="Name*" type="text" required="required" onblur="if (this.value == '') { this.value = 'Name*'; }" onfocus="if (this.value == 'Name*') { this.value = ''; }"/> <input id="home-worth-address" class ="validateMe" value="Address*" name="Address*" type="text" required="required" onblur="if (this.value == '') { this.value = 'Address*'; }" onfocus="if (this.value == 'Address*') { this.value = ''; }"/> <input id="home-worth-email" class ="validateMe" value="Email*" name="Email*" type="text" required="required" onblur="if (this.value == '') { this.value = 'Email*'; }" onfocus="if (this.value == 'Email*') { this.value = ''; }"/> <input id="home-worth-phone" value="Phone" name="Phone" type="tel" onblur="if (this.value == '') { this.value = 'Phone'; }" onfocus="if (this.value == 'Phone') { this.value = ''; }"/> <input id="home-worth-submit" name="home-worth-submit" type="submit" value="Send my estimate"/> <div id="home-worth-errorLabel" style="color: #fff; display: none; font-weight: bolder; padding: 1%;">Please fill out all required fields</div> <div id="home-worth-successLabel" style="color: green; visibility: hidden; font-weight: bolder; text-align: center">Request Submitted Sucessfully</div> </form>

<script> function formValidate() {

    var validateInput = $('.validateMe');

    validateInput.each(function () {
        var name = $('#home-worth-name').val();
        var address = $('#home-worth-address').val();
        var email = $('#home-worth-email').val();
        var phone = $('#home-worth-phone').val();
        var errorDiv = $('#home-worth-errorLabel');


            if (validateInput.val() != 'Name*') {
                name = $('#home-worth-name').val();
                $('#home-worth-name').css('border', '0');
                errorDiv.css('display', 'none');
            } else {
                $('#home-worth-name').css('border', '2px solid red');
                errorDiv.css('display', 'none');
                return false;
            }
            if (validateInput.val() != 'Address*') {
                address = $('#home-worth-address').val();
                $('#home-worth-address').css('border', '0');
                errorDiv.css('display', 'none');
                return false;
            } else {
                errorDiv.css('display', 'block');
                $('#home-worth-address').css('border', '2px solid red');
                return false;
            }
    });

    return false;
}

</script>

Steven Parker
Steven Parker
243,656 Points

When posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

1 Answer

Steven Parker
Steven Parker
243,656 Points

If you use the actual "placeholder" attribute instead of pre-filling "value", then the placeholder will never be submitted (unless the user types it in).

This will greatly simplify validation, and eliminate the need for all the blur/focus functions.