javascript - Validation of input elements haing name as array -
i have textbox , upload file like:
<input type="text" id = "contract_copy_text[]" name="contract_copy_text[]" value="" maxlength="50"/> <input type="file" name="contract_copy_pdf[]" id="contract_copy_pdf[]" accept="application/pdf" />
i not able validate it.
i have tried :
var contract_copy_text = $('#contract_copy_text[]').val(); // document.getelementbyid('contract_copy_text').value; var contract_copy_pdf = $('#contract_copy_pdf[]').val(); // document.getelementbyid('contract_copy_pdf').value; if (contract_copy_text == "") { alert("insert contract copy title file"); $('#err_lbl_contract_copy_text').html('insert contract copy title file'); return false; } if (contract_copy_pdf == "") { alert("please select contract copy pdf file upload"); $('#err_lbl_contract_copy_pdf').html('please select contract copy pdf file upload'); return false; }
but doesn't work.
please note: these fields dynamically generated required(on click). can tell me mistake making.
update:
please see fiddle
the problem in way wrote jquery selectors here:
var contract_copy_text = $('#contract_copy_text[]').val(); var contract_copy_pdf = $('#contract_copy_pdf[]').val();
you must escape [
, ]
characters (using \\
) because they have special meaning inside selector expression:
var contract_copy_text = $('#contract_copy_text\\[\\]').val(); var contract_copy_pdf = $('#contract_copy_pdf\\[\\]').val();
source https://api.jquery.com/category/selectors/ :
to use of meta-characters ( such !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) literal part of name, must escaped with 2 backslashes: \.
Comments
Post a Comment