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
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>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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