Question

Currently i have 5 radiobuttons. 4 of them define an certain amount of money. The last one marks a custom field there you can enter a custom amount of money in an input field.

Is it possible, just with HTML, to set the input field "required" only if the the last radiobutton is active.

If it's not possible, is javascript the best way to solve this problem ?

Was it helpful?

Solution

It's not possible to do conditional validation of form fields with HTML only. At most you could manipulate the DOM with javascript to add the required attribute to the <input> element when necessary (i.e. in an onchange event handler for the radio button).

Remember it might be useful to provide fallback behavior for the required attribute for older browsers without HTML5 support.

Note: This other question is pretty much about the same.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top