How to separate the rows of a textarea in different variables? JavaScript

1

I want to count the data separated by a comma (,) of a row of a textarea to verify if any data is empty, if it is empty the save button is disabled.

- Each row has a maximum of 3 data separated by a comma.

I have a function that when you sent a single row in the textarea is validating the blanks, but when you sent two or more the validation fails. Example:

When I send a row it verifies me correctly and activates the button.

family 1, group 1, description 1

When I send two and the second incomplete I take all the data as a single row and I activate the button (in this case the button should be disabled).

family 1, group 1, description 1

family 2, group 2

$("#copea").on("keyup", function() {

  obtener()

})

function obtener() {
  var variable = $("#copea").val();
  salto = variable.split("\n");
  for (var i = 0; i < salto.length; i++) {
    var row = salto[i];
    var primero = row.split(",")[0];
    var segundo = row.split(",")[1];
    var tercero = row.split(",")[2];
    if (primero != null) {
      var p = primero.replace(/^\s+|\s+$/gm, '');
    }
    if (segundo != null) {
      var s = segundo.replace(/^\s+|\s+$/gm, '');
    }
    if (tercero != null) {
      var t = tercero.replace(/^\s+|\s+$/gm, '');
    }
    if (p == null || s == null || t == null || p == "" || s == "" || t == "") {
      $("#IngCopear").attr("disabled", true);
    } else {
      $("#IngCopear").attr("disabled", false);
    }
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<textarea name="copea[]" id="copea" cols="79" style="max-width: 100%;max-height: 100%" rows="10" placeholder="familia 1, grupo 1, descripcion 1
familia 2, grupo 2, descripcion 2
..."></textarea>
   <button type="submit" id="IngCopear" name="IngCopear" class="btn btn-success copear pull-right">Guardar</button>
    
asked by Luis 20.07.2018 в 19:00
source

1 answer

1

You have to initialize the variables with null. Also, I would check for undefined instead of null . Something like this:

$("#copea").on("keyup", function() {

  obtener()

})

function obtener() {
  var variable = $("#copea").val();
  salto = variable.split("\n");
  for (var i = 0; i < salto.length; i++) {
    var row = salto[i];
    var primero = row.split(",")[0];
    var segundo = row.split(",")[1];
    var tercero = row.split(",")[2];
    var p = null;
    var s = null;
    var t = null;
    
    if (primero != undefined) {
      p = primero.replace(/^\s+|\s+$/gm, '');
    }
    if (segundo != undefined) {
      s = segundo.replace(/^\s+|\s+$/gm, '');
    }
    if (tercero != undefined) {
      t = tercero.replace(/^\s+|\s+$/gm, '');
    }
    if (p == null || s == null || t == null || p == "" || s == "" || t == "") {
      $("#IngCopear").attr("disabled", true);
      break;
    } else {
      $("#IngCopear").attr("disabled", false);
    }
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<textarea name="copea[]" id="copea" cols="79" style="max-width: 100%;max-height: 100%" rows="10" placeholder="familia 1, grupo 1, descripcion 1
familia 2, grupo 2, descripcion 2
..."></textarea>
   <button type="submit" id="IngCopear" name="IngCopear" class="btn btn-success copear pull-right">Guardar</button>
    
answered by 20.07.2018 / 19:13
source