开发者

JavaScript Question for IE6

I have a javascript requirement.

I will pass a comma separated string into a function. I need to ensure that it contains only integers (without decimals) and the value is less than 2147483648. Could you please help me ?

Note:: I am working on IE 6

Th开发者_StackOverflow中文版anks

Lijo


function validate(str){
    str=str.split(",")
    for(var a=0;a<str.length;a++){
        if(!str[a].match(/^[0-9]+$/)){
            return false
        }
        if(str[a]*1>=2147483648){
            return false
        }
    }
    return true
}

This doesn't accept negative integers or empty strings, should it?


parseInt() will handle the string -> integer conversion for you. As far as the figure goes, just test it using a conditional if/then:

var new_integer = parseInt(passedString);

if(new_integer < 2147483648){
    /* do something */
} else {
    /* do something else */
}


You can do something like this:

function getNumberArrayFromString(str) {

   var numbers = str.split(",");
   var numbersArr = new Array();

   for(var i = 0; i < str.length, i++) {
       var number = parseInt(str[i]);

       if(!isNan(number) && number < 2147483648) {
          numbersArr[numbersArr.length] = number;
          //You can also use numbersArr.push(number) but I'm not sure if that's supported in IE6
       }
   }

   return numbersArr;
}

Assuming I understand your question correctly.


function isValid(s){
  try { 
    return parseInt(s.replace(",","")) < 2147483648;
  } catch (e) {
    return false;
  }
}


Try this

function check(string){
    var s = string.split(',');
    for(i = 0; i <= s.length; i++){
        if(!isNaN(s[i]) && i >= 2147483648){
            return false
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜