0

i have a function which is supposed to check if a number is a floating-point number.

this is the structure of the function

function isValidFloat( $string ){
    if( !preg_match( "/^[0-9]+(.[0-9]+)?$/", $string ) ){
        return false;
    }
}

for numbers such as 1 or 8 it's correctly validating but not for floating point numbers such 19.25. where am i missing it

2
Contributors
3
Replies
11
Views
3 Years
Discussion Span
Last Post by pritaeas
0

the function still fails even if i modify the function to this

function isValidFloat( $string ){
        if( preg_match( "/^[0-9]+(.[0-9]+)?$/", $string ) ){
            return true;
        } else {
            return false;
        }
    }
0

Works for me. What version of PHP are you using?

<?php
function isValidFloat( $string ){
    if( preg_match( "/^[0-9]+(\.[0-9]+)?$/", $string ) ){
        return true;
    } else {
        return false;
    }
}

if (isValidFloat('19.25')) echo 'valid';
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.