javascript - one of the two input fields is required -
i have 2 input fields 1 file
other textarea
<input class="input_field" type="file" name="title" /> <textarea class="input_field" name="info"></textarea>
user has either upload file or type text. if user leaves blank both of inputs, should "choose file or type info" if he/she fills both, ok.
my jquery:
$(function(){ $(".input_field").prop('required',true); });
i have this code. how can implement if else
condition make required 1 of fields?
see fiddle https://jsfiddle.net/lez4r/652/
i modified code each elements class of input_field when form submitted.
$(function(){ $('form').submit(function (e) { var failed = false; $(".input_field").each(function() { if (!$(this).val()) { failed = true; } }); console.log(failed); if (failed === true) { e.preventdefault(); } }); });
Comments
Post a Comment