Skip to content

isValidNumber β€” GTM Variable Template for Logic

VARIABLES β€Ί LOGIC
isValidNumber EXTENDED Logic
Direct (.tpl) Apply (.tpl)

Checks if the provided value is valid number.



Number returns true
INPUT
Value To Check: 42
OUTPUT
true
Numeric string returns true
INPUT
Value To Check: 123.45
OUTPUT
true
NaN returns false
INPUT
Value To Check: 0/0
OUTPUT
false

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

isValidNumber
Value To Check
πŸ’Ύ The value to check.

Supported formats:
  βœ“ All
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Value To Check number
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
isValidNumber()


πŸ“œ View Implementation Code
/**
* Checks if the value can be interpreted as a valid number.
*
* @param {any} data.src - The value to check.
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform `src` before checking.
*
* @returns {boolean} True if the value can be interpreted as a valid number, false otherwise.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const isValidNumber = function(value) {
const num = makeNumber(value);
return num === num;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// isValidNumber - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(isValidNumber(value));
// ===============================================================================
// isValidNumber() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(isValidNumber(value));
};
*/
πŸ§ͺ View Test Scenarios (6 tests)
βœ… '[example] Number returns true'
βœ… '[example] Numeric string returns true'
βœ… Non-numeric string - should return false
βœ… '[example] NaN returns false'
βœ… Object - should return false
βœ… Infinity - should return true