stevieontario Posted May 30, 2013 Share Posted May 30, 2013 (edited) Afternoon freaks, I'm trying to customize a free bit of calculator code I got from the web. There's a regex in it that appears designed to prevent users from entering anything but digits in the input fields. However, it does allow "garbage" in, e.g. 55f x 45e = 2475 How can I redo the regex so that it only allows digits and one decimal? The regex uses eregi, which the PHP manual says has been deprecated. I don't think that's the main issue though. The code containing the regex is here: if(!eregi("[0-9]", $number1)){ echo("Number 1 MUST be numbers!"); exit; ... and this is the entire code: <?php // Calculator Script v1 // Copyright (C) 2007 RageD // Define to make this all one document $page = $_GET['page']; // Defining the "calc" class class calc { var $number1; var $number2; function add($number1,$number2) { $result =$number1 + $number2; echo("The sum of $number1 and $number2 is $result<br><br>"); echo("$number1 + $number2 = $result"); exit; } function subtract($number1,$number2) { $result =$number1 - $number2; echo("The difference of $number1 and $number2 is $result<br><br>"); echo("$number1 - $number2 = $result"); exit; } function divide($number1,$number2) { $result =$number1 / $number2; echo("$number1 divided by $number2 is $result<br><br>"); echo("$number1 ÷ $number2 = $result"); exit; } function multiply($number1,$number2) { $result =$number1 * $number2; echo("The product of $number1 and $number2 is $result<br><br>"); echo("$number1 x $number2 = $result"); exit; } } $calc = new calc(); ?> <TITLE>PHP Calculator v1</TITLE> <form name="calc" action="?page=calc" method="POST"> Number 1: <input type=text name=value1><br> Number 2: <input type=text name=value2><br> Operation: <input type=radio name=oper value="add">Addition <input type=radio name=oper value="subtract">Subtraction <input type=radio name=oper value="divide">Division <input type=radio name=oper value="multiply">Multiplication</input><br> <input type=submit value="Calculate"> </form> <?php if($page == "calc"){ $number1 = $_POST['value1']; $number2 = $_POST['value2']; $oper = $_POST['oper']; if(!$number1){ echo("You must enter number 1!"); exit; } if(!$number2){ echo("You must enter number 2!"); exit; } if(!$oper){ echo("You must select an operation to do with the numbers!"); exit; } if(!eregi("[0-9]", $number1)){ echo("Number 1 MUST be numbers!"); exit; } if(!eregi("[0-9]", $number2)){ echo("Number 2 MUST be numbers!"); exit; } if($oper == "add"){ $calc->add($number1,$number2); } if($oper == "subtract"){ $calc->subtract($number1,$number2); } if($oper == "divide"){ $calc->divide($number1,$number2); } if($oper == "multiply"){ $calc->multiply($number1,$number2); } } ?> I tried amending the regex inside the quotes to this: [\d.] ... but no dice. Edited May 30, 2013 by stevieontario Quote Link to comment https://forums.phpfreaks.com/topic/278609-likely-regex-eregi-problem-input-allows-non-digit-characters/ Share on other sites More sharing options...
requinix Posted May 30, 2013 Share Posted May 30, 2013 All your expression does is check the string contains a number. ctype_digit And avoid ereg/POSIX functions. They're old and deprecated and not as powerful as the preg/PCRE functions. Quote Link to comment https://forums.phpfreaks.com/topic/278609-likely-regex-eregi-problem-input-allows-non-digit-characters/#findComment-1433243 Share on other sites More sharing options...
stevieontario Posted May 30, 2013 Author Share Posted May 30, 2013 still alive, thanks. ctype looks like a promising way forward but man it looks like a lot of trial and error and testing! well, here goes... thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/278609-likely-regex-eregi-problem-input-allows-non-digit-characters/#findComment-1433272 Share on other sites More sharing options...
requinix Posted May 30, 2013 Share Posted May 30, 2013 Trial and error? Do you have another question besides how to check that something is a number? Quote Link to comment https://forums.phpfreaks.com/topic/278609-likely-regex-eregi-problem-input-allows-non-digit-characters/#findComment-1433277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.