Jump to content

Limit empty input array


balkan7

Recommended Posts

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
});
Link to comment
https://forums.phpfreaks.com/topic/292367-limit-empty-input-array/
Share on other sites

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>

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....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.