javascript - Radio button REQUIRED if input field has content -


i have a reasonably quick problem solve (i think). have form online , validates required content user's data, has no validation on first part of form.

i've been asked if can make radio button required depending on whether input field has been filled in.

the form can found here: http://www.elcorteingles.pt/reservas/livros_escolares/form.asp

so if person start's filling in input fields on first line, radio buttons in group become required (for either cdrom ou caderno not both)

you can handle focusout , blur events input:

$(function () {     // handle every input type text.     // select specific inputs, give them common class , change     // selector accordingly.      $("input[type=text]").on("focusout blur", function () {         // check inputs class radio_btns in         // parent element (li).         // set required property.          $(this).parent().find("input.radio_btns")             .prop("required", $(this).val().trim().length > 0);     }); }); 

demo

jquery reference (tree traversal)

jquery reference (.prop())

jquery reference (.focusout())

jquery reference (.blur())


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -