Search code examples
coldfusionintegercoldfusion-9isnumeric

Best way to check if value is integer ? Coldfusion 9


I have fields to test and make sure they only accept integers. There is few functions but I wasn't sure which one is the best. First I tried isValid("integer",value) but I have discovered that "1,5" will be accepted as an integer. So then I tried isNumeric(value) but this will accept values like 1.5. I'm wondering what should be the best way to check for integers? Maybe two combine these two functions like:

<cfif isValid("integer",value) AND isNumeric(value)>

Or there is better way to do this?


Solution

  • You could try this:

    value = replace(value, ',', '', 'all');
    numberIsInteger = isNumeric(value) && round(value) == value ? true : false;
    

    Note People often include commas in large numbers such as 1,000,000. isNumeric will return false for that string, as will the refind function in the other answers.