Jump to content

Help with form validation


sbridge

Recommended Posts

Greetings. I used this tutorial [https://www.w3schools.com/howto/howto_js_form_steps.asp] as the basis for a form that I am creating. Everything seems to be great, except for the fact that once I started to use form elements besides a text box (text area, multi-select) the validation doesn't work. I looked at the code but can't seem to figure it out.

Can someone help with adjusting the JavaScript validation to include other form elements? Also, is there an easy way to show all the inputted data on the last screen, as part of the confirmation? 

Thanks in advance!

form-test-2.html

Link to comment
Share on other sites

  • 2 weeks later...

Your validateForm function only checks input elements as it currently stands.

As a textarea element is not an input element, you'll need to add some additional code to the validateForm function.

You might be able to get some solid use out of element.querySelectorAll() in this case. Something close to

x = document.getElementsByClassName("tab");
y = x[currentTab].querySelectorAll("input");

If you modify what's inside the brackets of querySelectorAll, you may be able to use it get your inputtextarea, and select elements. Works just like a CSS selector.

Speaking of CSS, you'll also need to add an 'invalid' style for the textarea and select elements. Otherwise, it won't turn red like the others. You could copy the invalid style from the input.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...