Add characters before each number

2

I have a string in a textarea that will look like this:

2199998888
2188889999
2455556666
2566665555

That is, phone numbers with break line to separate them within the textarea.

I need to enter the country code on all phone numbers.

I could not find a simple logic to do this.

The result would be this:

552199998888
552188889999
552455556666
552566665555    
    
asked by anonymous 14.04.2015 / 16:02

1 answer

4

Just break the content into lines and add '55' to the beginning of each. So, for example:

var campo = document.querySelector('textarea');
var linhas = campo.value.split("\n");
var novo = linhas.map(function(item) {
  return item ? '55' + item : item; // se a linha estiver em branco, não acrescenta '55'
}).join('\n');
campo.value = novo;
<textarea rows="5">
2199998888
2188889999

2455556666
2566665555
</textarea>
    
14.04.2015 / 16:13