0

I have a problem in my form. In my form the user can add multiple discount codes. And what I need to validate are the two textboxes these are 'date start' and 'date expired' and I need to find out if these are empty. Before allowing the user to add another row again.

Here's my code:

function addProductDiscountCode() {

    var html  = '<tr id="row-count-' + iterator + '">';
        html += '   <td>';
        html += '       <input type="text" name="discount_code[' + iterator + '][code]" class="form-control" readonly="readonly" value="' + code + '" />';
        html += '   </td>';
        html += '   <td class="text-center">';
        html += '       <input type="text" name="discount_code[' + iterator + '][percentage]" class="form-control text-center" />';
        html += '   </td>';
        html += '   <td class="text-center">';
        html += '       <div class="input-group date"><input name="discount_code[' + iterator + '][date_start]" value="" id="start-date-' + iterator + '" placeholder="YYYY-MM-DD" data-date-format="YYYY-MM-DD" class="form-control date_set" type="text" readonly><span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span></div>';
        html += '   </td>';
        html += '   <td class="text-center">';
        html += '       <div class="input-group date"><input name="discount_code[' + iterator + '][date_expired]" value="" id="end-date-' + iterator + '" placeholder="YYYY-MM-DD" data-date-format="YYYY-MM-DD" class="form-control date_end" type="text" readonly><span class="input-group-btn"><button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button></span></div>';
        html += '   </td>';
        html += '   <td style="width: 15%">';
        html += '       <select class="form-control" name="discount_code[' + iterator + '][status   ]">';
        html += '           <option value="0">Disable</option>';
        html += '           <option value="1">Activate</option>';
        html += '       </select>';
        html += '   </td>';
        html += '   <td class="text-center"><button type="button" onClick="removeCode(' + iterator + ');" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
        html += '</tr>';

    $("#product-discount-code tbody").append(html);

    iterator++;

    $('.date').datetimepicker({
        pickTime: false
    });

}

And this will result in a row like this:

<tr id="row-count-1">
    <td>
        <input name="discount_code[1][code]" class="form-control" readonly="readonly" value="4npi7YIIrv" type="text">   
    </td>
    <td class="text-center">
        <input name="discount_code[1][percentage]" class="form-control text-center" type="text">  
    </td>
    <td class="text-center">       
        <div class="input-group date">
            <input name="discount_code[1][date_start]" value="" id="start-date-1" placeholder="YYYY-MM-DD" data-date-format="YYYY-MM-DD" class="form-control date_set" readonly="" type="text">
            <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
            </span>
        </div>  
    </td>   
    <td class="text-center">       
        <div class="input-group date">
            <input name="discount_code[1][date_expired]" value="" id="end-date-1" placeholder="YYYY-MM-DD" data-date-format="YYYY-MM-DD" class="form-control date_end" readonly="" type="text">
            <span class="input-group-btn">
                <button type="button" class="btn btn-default"><i class="fa fa-calendar"></i></button>
            </span>
        </div>   
    </td>   
    <td style="width: 15%">       
        <select class="form-control" name="discount_code[1][status   ]">           
            <option value="0">Disable</option>           
            <option value="1">Activate</option>       
        </select>   
    </td>   
    <td class="text-center">
        <button type="button" onclick="removeCode(1);" class="btn btn-danger">
            <i class="fa fa-minus-circle"></i>
        </button>
    </td>
</tr>

I added a console print in the add action:

$(".date_start").val();
$(".date_end").val();

And It only get the value of the first row. I need

2
  • Do you mean $(".date_set") instead of $(".date_start") ? Commented Apr 21, 2015 at 1:03
  • Ah you're right sorry. Commented Apr 21, 2015 at 1:15

1 Answer 1

1

The getter method like .val(), in jQuery will return the value of the first element in the set not of all the elements(with exceptions like .text())

You can use a loop based logic to see whether all the elements has values

var date_set = true;
$('.date_set').each(function () {
    if ($(this).val().length == 0) {
        date_set = false;
        return false;
    }
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.