1

How can I use javascript to check and see if a string has more than 1 integer?

ie.

var x = 1,15,30 { returns true }


var x = 13 { returns false }
Cœur
  • 37,241
  • 25
  • 195
  • 267
BobbyJones
  • 1,331
  • 1
  • 26
  • 44
  • The code you posted doesn't make sense. Where is the string? Did you mean to write `var x = '1,15,30'; // returns true` ? What is the format of the string? Are numbers always delimited by `,`? Can there be floats (e.g. `1.5`)? You are not providing enough information. – Felix Kling Dec 14 '16 at 18:43

2 Answers2

0

Assuming that all items are integers:

function(s){ return s.indexOf(',')!=-1 }
Artem Dudkin
  • 588
  • 3
  • 11
0

Use regex to search for groups of digits (\d+). If the length of all found groups is greater then 1 return true, if not return false.

function moreThenOneNumber(str) {
  return str.match(/\d+/g).length > 1;
}

var x = '1,15,30';
var y = '13';

console.log(moreThenOneNumber(x));

console.log(moreThenOneNumber(y));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209