开发者

Javascript in list

开发者 https://www.devze.com 2023-01-28 22:14 出处:网络
What\'s the easiest way to check to see if a number is in a comma delimited list? console.log(provider[cardType]);

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 >开发者_如何学Go 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;
}


Seems similar to this SO question.

Just .split() the CSV and use inArray.


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


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


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.


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消