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

Recommended Answers

All 3 Replies

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;
        }
    }

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';
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.