0

How can I count the unique number of phone numbers in a textarea separated with comma, newline, or space in javascript or jQuery? I have tried this code:

$('#gsmnumbers').on('keyup', function() {
    $('#num-counter').html($('#gsmnumbers').val().split(',').length);
}); 
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

2 Answers2

0

You can use a regular expression to split the string by multiple characters:

var count = $('textarea').val().split(/[\n,\s]/).length;

Example fiddle

Note that you may need to sanitise the input if it's user-entered as spaces are commonly used delimiters within a phone number itself, which may lead to an incorrect count.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

I was exactly searching for similar answer but using someone's fiddle, I just counted for comma, space, colon, semi colon, line breaks, etc., here is the code if someone in future needs it.

jsfiddle

var yay = $('textarea').val().split(/[\ \n\r\;\:\,]+/).length;
alert(yay);

Hope it helps someone.

yuri1000
  • 361
  • 4
  • 21