1

I have an odd situation, I think...

I am writing some jquery/javascript code to validate numbers and I need to check if a given int exists within the entered number.

So for instance if the number I am checking against is 15 and the persons types in a 1, I need to check and see if that 1 exists within the 15, if it does then return true else return false.

I tried .stringOf but that of course only works on strings.

Any thoughts? Sorry if it is a super simple answer. Thanks in advance.

So far:

var Quantity = parseFloat(entered_value); // 1
var max = 15;
if(max.indexOf(Quantity) >= 0){
    qty = Quantity;
}else{
    qty = max;
}
4

2 回答 2

3
var Quantity = parseFloat(entered_value); // 1
var max = 15;
if(max.toString().indexOf(Quantity.toString()) >= 0){
    qty = Quantity;
}else{
    qty = max;
}
于 2013-06-03T15:22:09.327 回答
1

只是为了输入原因的建议

var num = 1,
    max = 15,
    isSubString = (max+'').indexOf(num) > -1;

if ( isSubString ) {
  // Num is a sub string of max
} 
于 2013-06-03T15:17:57.610 回答