The MDN page on radio buttons states:
Validation. Radio buttons don't participate in constraint validation; they have no real value to be constrained.
But this isn't true. As a group they often need to be validated - e.g. if there is no default checked, but the user must check one of the options to continue. This is a commonly required UX to prevent a user submitting a form without making an active choice.
I found there is a RadioNodeList, which doesn't mention validation, but this doesn't seem to have the "willValidate()" method either. I was hoping to be able to set the required
attribute at that level, which I know isn't an HTML element, but if done in JS could in theory provide a native UX.
I wondered about copying the selected value into a hidden or text field and setting that field to required
but the UI might be confusing.
I know I can use JS, JQ etc. but I haven't been able to find a native method to validate a radio group.
Is there a native way?
UPDATE: This does just work as expected using the required
attribute - just tested in Safari, Chrome and Firefox. The reason I wasn't seeing it was due to a data-close
attribute on the submit button, which was hiding the form before the native validation prompts could be seen. And the MDN docs mislead me into thinking the field didn't support validation at all, whereas it's more specifically concerned with the constraint API.