0

What's the easiest way to check to see if a number is in a comma delimited list?

console.log(provider[cardType]);
    //returns: Object { name="visa", validLength="16,13", prefixRegExp=}

if (ccLength == 0 || (cardType > 0 && ccLength < provider[cardType].validLength)) {
    triggerNotification('x', 'Your credit card number isn\'t long enough');
    return false;
} else {
    if ($('.credit-card input[name="cc_cvv"]').val().length < 3) {
        triggerNotification('x', 'You must provide a CCV');
        return false;
}
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Ben
  • 60,438
  • 111
  • 314
  • 488

7 Answers7

1

Seems similar to this SO question.

Just .split() the CSV and use inArray.

Community
  • 1
  • 1
simshaun
  • 21,263
  • 1
  • 57
  • 73
1

Not sure how your sample code relates to checking to see if a number is in a comma delimited list...

Also not sure if this is the easiest way, but it's what springs to mind:

<script type="text/javascript">
var myNumbers = "1,2,3,4,5";
var myArray = myNumbers.split( ',' );

// looking for "4"
for ( var i=0; i<myArray.length; i++ ) {
    if (myArray[i] == 4) {
        alert('Found it!');
        break;
    }
}

charliegriefer
  • 3,342
  • 1
  • 18
  • 20
  • Per my answer and the question's inclusion of `validLength="16,13"` and `ccLength < validLength` I assume the question is not one of set membership, but rather range coverage. – Phrogz Dec 01 '10 at 21:46
1

I do not see where you have a significant comma delimited list in the script you posted. The fastest way could be something like

var csvList ="a,b,c,d,e,f,g,h";
var testList = ","+csvList+",";
var needle = "f";
alert(testList.indexOf(","+needle+",")!=-1)

just to be different ;)

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • This works for your test case, but not reliably for a larger set. – Jason McCreary Dec 01 '10 at 21:42
  • In what way? As long as there are no comma in the "needle" the this should work for a reasonably large set of values. Care to test this? – mplungjan Dec 01 '10 at 21:49
  • Sorry, there was another answer that took similar a similar appoach, with poor logic. You're right. By bordering the set and needle with commas, you should reliably match. +1 for the different approach. – Jason McCreary Dec 02 '10 at 02:27
0

If it's just a list of comma separated numbers with nothing fancy, you can just use the split method:

var numbers = list.split(",");

This will give you an array of all of the numbers in the list. Checking whether a number is in an array is trivial.

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
0

Native JavaScript and therefore cross-browser compliant. Some frameworks provide functions that do this for you, but you don't get more basic than the following.

var numbers = list.split(",");
var count = numbers.length;
var exists = false;

for (var i = 0; i < count; ++i) {
  if (numbers[i] == anumber) {
    exists = true;
    break;
  }
}
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
0

From your sample, I assume your question was "How do I see if a number is within a range of two values specified by a single-comma-delimited string?":

function inRange( number, stringRange ){
  var minmax = stringRange.split(',');
  minmax[0] = minmax[0]*1; //convert to number
  minmax[1] = minmax[1]*1; //convert to number
  minmax.sort(); // Ensure [0] is the min
  return number>=minmax[0] && number<=minmax[1];
}
Phrogz
  • 296,393
  • 112
  • 651
  • 745
0

Try this one...

 console.log(provider[cardType]); //returns: Object { name="visa", validLength="16,13", prefixRegExp=} 
var regExp = new RegExp(",?" + ccLength + ",?");
if (ccLength == 0 || (cardType > 0 && !regExp.test(provider[cardType].validLength))) 
{ 
    triggerNotification('x', 'Your credit card number isn\'t long enough'); 
    return false; 
} 
else 
{ 
    if ($('.credit-card input[name="cc_cvv"]').val().length < 3) 
    { 
            triggerNotification('x', 'You must provide a CCV'); 
            return false; 
    } 
}               
Chandu
  • 81,493
  • 19
  • 133
  • 134