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;
}

7 Answers 7

1

Seems similar to this SO question.

Just .split() the CSV and use inArray.

Sign up to request clarification or add additional context in comments.

Comments

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;
    }
}

1 Comment

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.
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 ;)

3 Comments

This works for your test case, but not reliably for a larger set.
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?
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.
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.

Comments

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;
  }
}

Comments

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];
}

Comments

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; 
    } 
}               

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.