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;
}
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
klye_g
  • 1,242
  • 6
  • 31
  • 54
  • 6
    try converting it to a string to do the check. You can do that easily like this: var maxStr = "" + max, then check maxStr with indexOf – Reason Jun 03 '13 at 15:14
  • 2
    I would suggest that if you're trying to check for a float value as a substring within a string representation of an integer, then there's a possibility you're going about solving something in a less than ideal way. What is the actual problem you're trying to solve, in context? – Pudge601 Jun 03 '13 at 15:25

2 Answers2

3
var Quantity = parseFloat(entered_value); // 1
var max = 15;
if(max.toString().indexOf(Quantity.toString()) >= 0){
    qty = Quantity;
}else{
    qty = max;
}
Arun
  • 3,036
  • 3
  • 35
  • 57
1

Just to type out Reason's suggestion

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

if ( isSubString ) {
  // Num is a sub string of max
} 
Community
  • 1
  • 1
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123