jquery - multiple forms in a page - check for empty field in a different form from the one submitted -


i have 2 different forms, while submitting form #orchestrationform, want see if filepaths text box in form #blah empty. how in jquery?

my html , jquery follows:

<form id="blah"></form> <form method="post" action="/api/wand" enctype="multipart/form-data" id="orchestrationform">   <section id="data-files">     <input type="text" class="form-control" name="filepaths" form="blah" />   </section> </form> 

$("#orchestrationform").on("submit", function() {    var nofiles = false;    $("[name='filepaths']", this).each(function() {      if (!$(this).val()) {        nofiles = true;      }    });    if (nofiles) {      alert("upload atleast 1 file");    }    return false;  });

while submitting orchestration form searching name attribute filepaths, if empty, showing alert box. problem is, when filepaths text box not empty, nofiles evaluates true , alert box gets shown. wrong jquery?

it's not clear you're doing, suspect you're building form upload multiple files. created working example: https://jsfiddle.net/twisty/p75tzcvj/

html

<form id="blah">   <h2>   form 1   </h2> </form> <form method="post" action="/api/wand" enctype="multipart/form-data" id="orchestrationform">   <h2>   form 2   </h2>   <section id="data-files">     <input type="text" class="form-control" name="filepaths" form="blah" />   </section>   <button type="submit">upload</button> </form> 

jquery

$(document).ready(function() {   $("#orchestrationform").on("submit", function() {     var nofiles = false;     $("input[name='filepaths']", this).each(function() {       if ($(this).val() === "") {         nofiles = true;       }     });     if (nofiles) {       alert("upload atleast 1 file");     }     return false;   }); }); 

if field empty, alert displayed. !$(this).val() equate true in both cases. use $(this).val() === "" when not empty, return false. in example, empty field provides alert, non-empty field not provide alert, form fails due return false.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -