0

I am trying to validate the dynamically added input type field which are in a row of a table on button click event. The very first input type field has an ID so I am able to put a validation on it. But when I add more rows in the table dynamically , I don't know how to validate those input types of rows.

HTML :

<table class="table table-bordered table-hover" id="tab_logic">
                <thead>
                    <tr>

                        <th class="text-center">Channel Group Name
                        </th>
                        <th class="text-center">Description
                        </th>

                    </tr>
                </thead>
                <tbody>
                    <tr id='addr0'>

                        <td id="CGN">
                            <input type="text" id="channelgrpName" name='channelgrpName' placeholder='Channel Group Name' class="form-control" />
                        </td>
                        <td id="DES">
                            <input type="text" id="description" name='description' placeholder='Description' class="form-control" />
                        </td>

                    </tr>
                    <tr id='addr1'></tr>
                </tbody>
            </table>

Jquery :

$(document).ready(function () {

    var i = 1;
    $("#add_row").click(function () {

        $('#addr' + i).html("<td><input name='name" + i + "' type='text' placeholder='Channel Group Name' class='form-control' style='width:50%'  /> </td><td><input  name='mail" + i + "' type='text' placeholder='Description'  class='form-control' style='width:50%'></td>");

        $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
        i++;
    });
    $("#delete_row").click(function () {
        if (i > 1) {
            $("#addr" + (i - 1)).html('');
            i--;
        }
    });
});

$('#btnSave').click(function () {

 // what should i to do here to validate dynamically added input types in row

});

Thanks in advance.

7
  • 1
    what are you currently using for validation? and what type of the validation you need Commented May 26, 2016 at 6:31
  • Use classes instead of Ids and validate referring to class name and $this object Commented May 26, 2016 at 6:37
  • @Thorin - I want jquery validation here like by checking the input type value , if its null , it will show the error. Commented May 26, 2016 at 6:40
  • @anu: can you please show me an example how to do that. Commented May 26, 2016 at 6:41
  • For that you need to show in your post what kind of validation you are doing Commented May 26, 2016 at 6:42

3 Answers 3

1

Just try this, if you need only check blanks

Like this:

$('#btnSave').click(function () {
  var validate = true;
    $('#tab_logic').find('tr input[type=text]').each(function(){
    if($(this).val() == ""){
        validate = false;
    }
  });
  if(validate){
    alert("Form is validate")
    return true;// you can submit form or send ajax or whatever you want here
  } else {
    alert("please fill all the details")
    return false;

  }
});

Js Fiddle for check blank fileds

And if you need more validation just use some js plugins to validate like

jQuery-Validation-Engine

Sign up to request clarification or add additional context in comments.

Comments

1
please check this link i hope this will helpful for you. 

https://jsfiddle.net/ffgbvsg0/

Comments

0

You don't need ids at all.

In your first row and when you append new rows, give them a class for eg: added-row.

And bind the click listener on #tab_logic tbody tr.added-row

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.