It explains some of the code examples above as well as introducing more advanced concepts: Very new to coding. Would be great if you could post full examples of code including form and not just pieces that fit into the greater framework of code.I've coded a working form, all checks are great but for some reason the hardest part is trying to stop form submission before validation.Tried setting it up on onsubmit on both form and button and no go.
Radio buttons are implemented as if they were an array of checkboxes.
When form input is important, it should always be verified using a secure server-side script.
Otherwise a browser with Java Script disabled, or a hacker trying to compromise your site, can easily submit invalid data. The first test in the example is therefore only necessary in order to provide a different error message when the input is blank.
To find out which value (if any) has been selected, you need to loop through the array until you find which one has been selected: In the case of a single radio button we have nothing to loop through so just return either it's value, if the radio button is checked, or false.
If you're working with arrays of checkboxes to submit data to a server-side script then you might already have some grey hairs from trying to figure out how to validate the input using Java Script.