0

Im checking for validation for both integer and float. preg('/^[0-9]+(.[0-9]+)?$/',$str) This works fine 12,12.05 But not for .12

Any suggestions?

Thanks in advance, kumar.

6 Answers 6

5

Make that

preg('/^[0-9]*(\.[0-9]+)?$/',$str)

+ means the element will be repeated one or more times, * means it will be repeated zero or more times.

Sign up to request clarification or add additional context in comments.

1 Comment

You need to escape ., and this regex matches empty string.
1

Yep try this:

if((int) $str != $str)
{
    //error
}

if((float) $str != $str)
{
    //error
}

This should do the trick, and I suppose it will work faster than the regex. Additonally you can check after that (float) $str <= 0 -> another error, etc.

I would bet on regex for more complicated validations, where the with standard routines you have to write some chunk of code, such as email validation, mask(pattern) input and so on.

Comments

1

You could use a combination of is_float and is_int

however, both are pretty much covered by is_numeric

<?php
$tests = array(
    "42", 
    1337, 
    "1e4", 
    "not numeric", 
    array(), 
    9.1
);

foreach ($tests as $element) {
    if (is_numeric($element)) {
        echo "'{$element}' is numeric", PHP_EOL;
    } else {
        echo "'{$element}' is NOT numeric", PHP_EOL;
    }
}
?>

Output:

'42' is numeric
'1337' is numeric
'1e4' is numeric
'not numeric' is NOT numeric
'Array' is NOT numeric
'9.1' is numeric

Is Float Example:

<?php
if (is_float(27.25)) {
    echo "is float\n";
} else {
    echo "is not float\n";
}
var_dump(is_float('abc'));
var_dump(is_float(23));
var_dump(is_float(23.5));
var_dump(is_float(1e7));  //Scientific Notation
var_dump(is_float(true));
?>
is float
bool(false)
bool(false)
bool(true)
bool(true)
bool(false)

and Is_int example:

<?php
if (is_int(23)) {
    echo "is integer\n";
} else {
    echo "is not an integer\n";
}
var_dump(is_int(23));
var_dump(is_int("23"));
var_dump(is_int(23.5));
var_dump(is_int(true));
?>
is integer
bool(true)
bool(false)
bool(false)
bool(false)

Comments

0
<?php
$regex = '~^(\d+)|(\d+)?\.(\d+)$~';
$input = array( '12', '12.05', '.12' );

foreach( $input as $string ) {
    echo "Match " . ( preg_match( $regex, $string ) !== 0 ? "OK" : "NOT OK" ) . "\n";
}

Comments

0

you can use is_float and is_int function of PHP to validate number..

Comments

0

I suggest is_numeric function in PHP that has very better performance than regex functions.

is_numeric return true for ALL of these items:

  1. integer $s = 123;
  2. string that contain integer $s = '123';
  3. float integer $s = 123.456;
  4. float integer whitout leading zero $s = '.456';
  5. negative number $s = '-.456';

you can see it in action here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.