joker53142 Posted September 8, 2014 Share Posted September 8, 2014 I have to do an assignment for school where I create an html page and a separate page for php. The code is to sum two numbers and return an error if the value entered is not numeric. The sum portion is working; however the numeric validation is not. The code is here: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Enter your information in the form below</title> </head> <body> <!-- Form2.html --> <form action="Form2.php" method="post"> <fieldset><legend> Enter a number in the form below:</legend> <p><b>number 1: <input type="text" name="number1" size="4" maxlength="4" /></b></p> <p><b>number 2: <input type="text" name="number2" size="4" maxlength="4" /></b></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"/></div> </form> </body> </html> <html> <body> <?php $number1 = $_POST['number1']; $number2 = $_POST['number2']; $sum = $number1+$number2; if (!empty($number1)&& !empty($number2)) { if(!is_numeric($number1)) { echo "The value you entered for number 1 is not a number. Please enter a numeric value for number 1"; } else if(!is_numeric($number2)) { echo "The value you entered for number 2 is not a number. Please enter a numeric value for number 2"; } else { echo "The sum of the numbers is: ".$sum; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/ Share on other sites More sharing options...
requinix Posted September 8, 2014 Share Posted September 8, 2014 The validation isn't working... how? How is it not working? What is it doing and what is it supposed to be doing? Anyways, your validation logic is a bit screwy. If both numbers have values then the sum won't show. Oh, and keep in mind that "0" will be considered empty. Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/#findComment-1490284 Share on other sites More sharing options...
jcbones Posted September 8, 2014 Share Posted September 8, 2014 There are a couple of different options that you need to look at. 1. Is the number always going to be an integer, or can a float be used? 2. Do you need to add type casting to it, or are you just going to test the string. is_numeric will return true on more than just integers or floats, and possibly allow results that you do not want. There are however a list of functions that test strings to see if they are specific types, a quick search of the manual should yield results (perhaps "character type checking"). However, these listing of functions will not pass a float. If that is an area that you wish to explore, perhaps you are stuck with a regex pattern to get the correct responses. Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/#findComment-1490285 Share on other sites More sharing options...
jeffreyappel Posted September 8, 2014 Share Posted September 8, 2014 Just tried your code in my local openserver, the validation works well. Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/#findComment-1490293 Share on other sites More sharing options...
Richard_Grant Posted September 8, 2014 Share Posted September 8, 2014 you have a lot of options here,,, You can check the string is it contains a character other than a number. you can use is_nummeric http://php.net/manual/en/function.is-numeric.php (i suggest this) you can use is_int http://php.net/manual/en/function.is-int.php (i suggest this) you can use is_string http://php.net/manual/en/function.is-string.php a cheap way to do this could be: (i wouldnt suggest this) $allow = "1234567890."; //Remove end decimal if you dont wish to allow decimals $myvalue = 1; $allow_split = str_split($allow); for($i =0; $i < count($allow_split); $i++){ if(strpos($allow_split[$i], (string)$myvalue)) { // i only wrote this for single didget numbers you can write another for loop to loop trough your myvalue variable just like i did with the allowed variables. } } Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/#findComment-1490299 Share on other sites More sharing options...
mac_gyver Posted September 8, 2014 Share Posted September 8, 2014 @Richard_Grant, is_int and is_string test the type of a variable, not what's in a variable, and by definition all $_POST data are string variables, no matter what's in them. 1 Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/#findComment-1490323 Share on other sites More sharing options...
kicken Posted September 8, 2014 Share Posted September 8, 2014 you can use is_nummeric http://php.net/manual/en/function.is-numeric.php (i suggest this)They are using this. As mentioned though, this will allow things that some people would not consider to be a valid number, such as 0xBEEF. you can use is_int http://php.net/manual/en/function.is-int.php (i suggest this) you can use is_string http://php.net/manual/en/function.is-string.php Neither of these will help. They test the data type of the variable, not it's contents. All form data is a string, so is_string would be true and is_int would be false regardless of the actual values they contain. @joker53142 ctype_digit is one way to check for a numeric value if you do not need to allow decimal points or negative numbers. Otherwise you'll probably want to use a regex with preg_match or check individual characters such as with a loop and list of valid characters as shown above. <?php $regex = '/^-?[0-9]+(\.[0-9]+)?$/'; if (preg_match($regex, $number1) && preg_match($regex, $number2)){ //valid numbers } The regular expression above checks that a string consists of only digits and optional a - at the start for negatives and a optional . followed by more digits for fractions. 1 Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/#findComment-1490329 Share on other sites More sharing options...
Richard_Grant Posted September 9, 2014 Share Posted September 9, 2014 @Richard_Grant, is_int and is_string test the type of a variable, not what's in a variable, and by definition all $_POST data are string variables, no matter what's in them. I didn't thoroughly read his post, i just got the jist and posted some options that are relevant. If your using a $_POST and you know that type should be an int, perhaps the first thing you should do is make sure its and INT before continuing and ten convert the type to an INT. Quote Link to comment https://forums.phpfreaks.com/topic/290911-number-validation/#findComment-1490431 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.