Increment input values in +1 containing letters and numbers using JQuery?

4

I have a table with data from check sheets like: bank, account, agency and sheet number, everything was fine so far, but today a customer came with an itau receipt and it has 3 letters followed by a hyphen and the number sequence. example: ABC-7852

The numbers of the sheets are sequential incremented by +1, in the sheets of itaú the letters do not change, I tried using the function explode do php and I was able to make it work disregarding everything before (-) and then concatenating.

No JQuery can I use explode or some similar function? To repeat the letters in all the lines and to increment only the numeric values.

ABC-7852
ABC-7853
ABC-7854
ABC-7855

follows the current code.

$(document).on('blur', '.ncheque', function() {
  var chqs = $('.ncheque');
  var index = null;
  var valor = parseInt(this.value);
  indice = chqs.index(this);
  if (valor) {
    $('.ncheque').each(function(index) {
      if (index > indice) {
        $(this).val(valor + index);
      }
    });
  } else {
    $('.ncheque').val('');
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="form-horizontal">
  <!-- Text input-->
  <div class="form-group">
    <label class="col-md-4 control-label" for="txtCh1"></label>
    <div class="col-md-4">
      <input id="txtCh1" name="txtCh1" type="text" placeholder="Folha cheque 1" class="form-control input-md ncheque">
      <input id="txtCh2" name="txtCh2" type="text" placeholder="Folha cheque 2" class="form-control input-md ncheque">
      <input id="txtCh3" name="txtCh3" type="text" placeholder="Folha cheque 3" class="form-control input-md ncheque">
      <input id="txtCh4" name="txtCh4" type="text" placeholder="Folha cheque 4" class="form-control input-md ncheque">
    </div>
  </div>
</div>
    
asked by anonymous 20.09.2017 / 18:29

3 answers

6

The PHP corresponding to explode of PHP is split .

To use both cases to have only numbers or numbers after letters (with the - tab) you can do this:

  var partes = this.value.split('-');
  var valor = parseInt(partes[1] || partes[0]);

and later to put it back you can have this logic:

[partes[1] ? partes[0] : null, valor + index].filter(Boolean).join('-')

The idea is to put partes[0] back in case partes[1] exists.

Example:

$(document).on('blur', '.ncheque', function() {
  var chqs = $('.ncheque');
  var index = null;
  var partes = this.value.split('-');
  var valor = parseInt(partes[1] || partes[0]);
  indice = chqs.index(this);
  if (valor) {
    $('.ncheque').each(function(index) {
      if (index > indice) {
        $(this).val([partes[1] ? partes[0] : null, valor + index].filter(Boolean).join('-'));
      }
    });
  } else {
    $('.ncheque').val('');
  }
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="form-horizontal">
  <!-- Text input-->
  <div class="form-group">
    <label class="col-md-4 control-label" for="txtCh1"></label>
    <div class="col-md-4">
      <input id="txtCh1" name="txtCh1" type="text" placeholder="Folha cheque 1" class="form-control input-md ncheque">
      <input id="txtCh2" name="txtCh2" type="text" placeholder="Folha cheque 2" class="form-control input-md ncheque">
      <input id="txtCh3" name="txtCh3" type="text" placeholder="Folha cheque 3" class="form-control input-md ncheque">
      <input id="txtCh4" name="txtCh4" type="text" placeholder="Folha cheque 4" class="form-control input-md ncheque">
    </div>
  </div>
</div>
    
20.09.2017 / 18:39
5

Use split (explode) and join (implode)

var str = "ABC-7852";
var res = parseInt(str.split("-")[1]) + 1;
res = "ABC-" + res;
console.log(res);
    
20.09.2017 / 18:39
4

The function name is Split. It can be read better in this LINK .

In it, you will have a certain String, and you can break it according to the character you want, in your case, the "- (dash)".

The function returns you an Array of X sizes where your string will be broken, in your case (ABC-7852), an array of size 2 will be returned, something like this:

Array [0] = ABC, Array [ 1 ] = 7852

Finally, just count both and return the String you want, in the order you want. Example:

var string = "ABC-7852";
var retorno = string.split('-');
console.log(retorno);
    
20.09.2017 / 18:40