balkan7 Posted November 8, 2014 Share Posted November 8, 2014 Hi How can limit only required input fields cannot be empty. <form name="test" method="post" action="test.php"> <label for="quest"> <input type="text" name="quest" /> <!-- REQUIRED --> </label> <label for="answers"> <input type="text" name="answers[]" /> <!-- REQUIRED --> </label> <label for="answers"> <input type="text" name="answers[]" /> <!-- REQUIRED --> </label> <label for="answers"> <input type="text" name="answers[]" /> </label> <label for="answers"> <input type="text" name="answers[]" /> </label> <label for="answers"> <input type="text" name="answers[]" /> </label> <label for="submit"> <input type="submit" name="submit" value="Submit" /> </label> </form> Following function working only on all empty input fields, i need help to enable button when required fields is not empty. $(document).ready(function() { var $submit = $("input[type=submit]"), $inputs = $('input[name=quest], input[name=answers[]]'); function checkEmpty() { // filter over the empty inputs return $inputs.filter(function() { return !$.trim(this.value); }).length === 0; } $inputs.on('keyup', function() { $submit.prop("disabled", !checkEmpty()); }).keyup(); // trigger an initial blur }); Quote Link to comment https://forums.phpfreaks.com/topic/292367-limit-empty-input-array/ Share on other sites More sharing options...
hansford Posted November 8, 2014 Share Posted November 8, 2014 Try this: $inputs = $('.required-fields'); Then add a generic class to the required fields. <label for="quest"> <input type="text" name="quest" class="required-fields" /> <!-- REQUIRED --> </label> <label for="answers"> <input type="text" name="answers[]" class="required-fields" /> <!-- REQUIRED --> </label> <label for="answers"> <input type="text" name="answers[]" class="required-fields" /> <!-- REQUIRED --> </label> Quote Link to comment https://forums.phpfreaks.com/topic/292367-limit-empty-input-array/#findComment-1496125 Share on other sites More sharing options...
balkan7 Posted November 8, 2014 Author Share Posted November 8, 2014 Hi hansford Thanks for helping but i think this is not the best solution because if i had in database question with 5 answers and when i edit this will look same: <? foreach($result as $data) { echo '<input type="text" name="answers[]" class="required-fields" value="'.$data['answer'].'" />'; } ?> I think is better to use for loop for limit inputs.... Quote Link to comment https://forums.phpfreaks.com/topic/292367-limit-empty-input-array/#findComment-1496128 Share on other sites More sharing options...
hansford Posted November 9, 2014 Share Posted November 9, 2014 You wouldn't need to loop through them if I'm understanding you correctly - and probably not. You can select the required fields in the answers[] array too: $('input.required-fields') Quote Link to comment https://forums.phpfreaks.com/topic/292367-limit-empty-input-array/#findComment-1496148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.