var age = parseInt( document.getElementById("age").value ); // This one ↑
if( isNaN( age ) ) { // true, so not a valid number... Click to run ->
alert( "Please enter a valid age!" );
document.getElementById("age").focus();
} else
alert( "Thanks for confirming your age" );
Tip: a blank field (no value supplied) will also return NaN. If a user enters fractional age (like 21.5 for 21 and a half years old), JavaScript will have "21" as age, because we used parseInt().
function isNumber(num) { return !isNaN(num) } // true if you passed a number
Note: the exclamation point ! ("unary negation operator") returns the opposite boolean value.
$age = '10 years old';
if( is_numeric( $age ) )
echo 'Thanks for entering your age!';
else
echo 'Please enter a valid age.';
Tip: PHP also includes the is_int() and is_float() functions, if you need more detailed info!