Jump to content
sbridge

Help with form validation

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...