Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to validate decimal numbers in PHP

Tags:

validation

php

how to validate decimal numbers in PHP. I looked at is_numeric() but that won't work for me:

bool is_numeric ( mixed var )

Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

I don't want the exponent part or hexadecimal notation. The user will be entering in simple decimal values and I don't want a type-o that just happens to be a valid exponent or hex value to slip past. I'd just like "traditional" decimal numbers to be consdered valid.

EDIT here a simple (brute force) page that contains much more complete test data (what should and should not be considered a numeric value).

<html><head></head>
<body>

<?php

function TestFunction($s_value) {
    //
    //  your code here
    //
    return; //true or false;
}

print '<b>these are valid numbers and should return "true"</b><br>';
print '<pre>';
    $s_value='123';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='+1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='-1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1  ';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  1  ';   print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='12345.1'; print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='6789.01'; print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='-1.1';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='+1.1';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='00001.1'; print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.0000001';print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='5.';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
print '</pre>';

print '<br><b>these are NOT valid numbers and should return "false"</b><br>';
print '<pre>';

    $s_value='--------------------------------';print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value=null;      print "\n".'$s_value=null, TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='.';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='';        print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value=' ';       print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='  ';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1abc';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='$1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1@';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1.2.1';   print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='abc';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1.45e6';  print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0xFF';    print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='++1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='--1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1+';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='1-';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='a1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='#1';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='10.e5';   print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0x1';     print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
    $s_value='0x';      print "\n".'$s_value="'.$s_value.'", TestFunction()='.(TestFunction($s_value)?'true':'false');
print '</pre>';

?>

</body>
</html>
like image 313
xyz Avatar asked Nov 12 '10 16:11

xyz


People also ask

How can I check if a number is decimal in PHP?

How do you check if number is decimal or not PHP? The is_numeric() function checks whether a variable is a number or a numeric string. This function returns true (1) if the variable is a number or a numeric string, otherwise it returns false/nothing.

How do you validate decimals?

Using an if-else statement to validate decimal numbers We have used the indexOf() method to check whether the decimal is present in the number or not. If it is present, it should not be in the first place. Users can follow the below syntax to use regular expressions to validate decimal numbers.

Is a decimal and integer in PHP?

PHP IntegersAn integer is a number without any decimal part. An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems.

What is form validation in PHP?

Form Validation is a necessary process before the data entered in the form is submitted to the database. This is done to avoid unnecessary errors. In PHP Form validation, the script checks for data in respective fields based on the rules set by the developer, and returns an error if it does not meet the requirements.


2 Answers

Updated with your test data.

function TestFunction($s_value) {
    $regex = '/^\s*[+\-]?(?:\d+(?:\.\d*)?|\.\d+)\s*$/';
    return preg_match($regex, $s_value); 
}

$valid = TestFunction($input);

Or trim the input first

function TestFunction($s_value) {
    $regex = '/^[+\-]?(?:\d+(?:\.\d*)?|\.\d+)$/';
    return preg_match($regex, $s_value); 
}

$input = trim($input);
$valid = TestFunction($input);
like image 139
rr. Avatar answered Oct 15 '22 17:10

rr.


$decimal = preg_match('/^[+\-]?\d+(\.\d+)?$/', $value) ? (float)$value : 0.0;
like image 34
Ken Richards Avatar answered Oct 15 '22 18:10

Ken Richards