Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Non-Jquery Form Validation

Hello there,

I've been working on my first real node project with express and it's been going really well thanks to the courses here at Treehouse.

Part of the project is a multi-step form with client-side validation. I've created these in the past successfully with the help of jquery but I decided early on in this project that I didn't want to rely on jquery. Unfortunately, most of the really good form validation plugins are written in jquery. Parsley and jquery validation plugin

Here is my working jquery code using jquery validation plugin:

$('#responsive-form .btn-next, .btn-banner').on('click', function() { 
        parent_fieldset = $(this).parents('fieldset');
        form = $("#responsive-form")
        form.validate({ 
            errorClass: "has-error",
            validClass: "has-success",
            highlight: function( element, errorClass, validClass ) {
                $(element).closest('.form-group').addClass(errorClass).removeClass(validClass);
            },
            unhighlight: function( element, errorClass, validClass ) {
                $(element).closest('.form-group').removeClass(errorClass).addClass(validClass);
            },
            rules: {
                "currentZipCode": {
                    required: true, 
                    number: true, 
                    maxlength: 5, 
                    minlength: 5, 
                }, 
                "property-state": {required: true},
                "firstName": {
                    required: true,
                    minlength: 2,
                    url: false,
                    number: false,
                },
                "lastName": {
                    required: true,
                    minlength: 2,
                    url: false,
                    number: false,
                },
                "eMail": {
                    required: true,
                    email: true,
                },
                "phoneNumber": {
                    required: true
                },
            },
            messages: {
                "currentZipCode": "Please enter a valid zip code",
                "property-state": "Please select a state",
                "firstName": "Please enter your first name",
                "lastName": "Please enter your last name",
                "eMail": "Please enter a valid email address",
                "phoneNumber": "Please enter a valid phone number",
            },
            tooltip_options: {
                "currentZipCode": {placement: 'bottom'},
                "property-state": {placement: 'bottom'},
                "firstName": {placement: 'bottom'},
                "lastName": {placement: 'bottom'},
                "eMail": {placement: 'bottom'},
                "phoneNumber": {placement: 'bottom'},
            },
        });
        if (form.valid() === true) {
            $(this).parents('fieldset').fadeOut(400, function() {
                $(this).next().fadeIn();
            })
}

It confirms that the input on each "page" is valid before allowing the next button to be clicked.

On my new site, using vanilla javascript, I've got the next button working and it moves the form along to the next field group, however, I've been wildly unsuccessful finding a good way to validate each field within the field groups. Any suggestions or help would be GREATLY appreciated. Again, I'm trying to avoid using jquery if I can.

//Banner Form next button behavior
  for (i=0; i < nextButton.length; i++) {
    nextButton[i].addEventListener('click', function(){
      var form = document.getElementById('bannerForm')

      //if (valid){
        this.parentNode.style.display= "none";
        this.parentNode.nextElementSibling.style.display="table";
      //}
    })
  }

1 Answer