Validate decimal places on a form with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Validate decimal places on a form with php

Post by cjkeane »

hi everyone.

i need a way to validate that both a decimal is entered in one textbox on a form and a rate in another textbox. for eg.
amount in currency textbox: 1000.25
amount in rate texbox: .0021

i perform a calculation on these fields so i need to make sure no extra characters like commas or dollar signs are typed in the textboxes. what is the best way to do this. i thought isnumeric would work, but it doesnt allow decimal places.

any ideas? thanks.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Validate decimal places on a form with php

Post by andym01480 »

is_numeric() does return true for strings with decimal places
prensil
Forum Newbie
Posts: 15
Joined: Tue Apr 26, 2011 8:38 am
Location: Ahmedabad
Contact:

Re: Validate decimal places on a form with php

Post by prensil »

Is_numeric will definately work here.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: Validate decimal places on a form with php

Post by cjkeane »

thanks i did manage to get the check to perform correctly. i'm wondering if there is a better way to perform the following routine?

Code: Select all

<?php
   if (!is_numeric($NumberOfDaysTOCalc)) {
       echo "<B>Validation Errors:</B><br /><br />";
       echo '<div style="color:red;">The Number of Days field is not numeric.</div><br />';
    } else {
    if (!is_numeric($UnitCost)) {
        echo "<B>Validation Errors:</B><br /><br />";
        echo '<div style="color:red;">The Unit Cost field is not numeric. </div><br />';
     } else {
     if (!is_numeric($TotalCostToDateInput)) {
         echo "<B>Validation Errors:</B><br /><br />";
         echo '<div style="color:red;">The Cost of Service TTD field is not numeric. </div><br />';
      } else {
      if (!is_numeric($ExchangeRate)) {
          echo "<B>Validation Errors:</B><br /><br />";
          echo '<div style="color:red;">The Exchange Rate field is not numeric. </div><br />';
  } else {
 //all data has been verified, so save data into database 
?>
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Validate decimal places on a form with php

Post by andym01480 »

I do it something like this

Code: Select all

$errors=array();//array to hold error messages
//do your error checking and populate $errors
if(!is_numeric($x)) $errors[$x]='error message for x';
if(!is_numeric($y)) $errors[$y]='error message for y';
//etc etc and other error checking

if(empty($errors))
{//no errors
//process
}//end of no errors
else
{//errors
//show the errors and the form again etc...
echo'<p><strong>There were some errors...</strong><br/>';
foreach($errors AS $key=>$value){ echo $value.'<br/>';}
echo'</p>';
//reoutput form etc...
}//end of errors
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: Validate decimal places on a form with php

Post by cjkeane »

thanks! i think that is a great way to perform the check. its actually a better method. also, what if $x, or $y is null? by that i mean, if the value entered in the $x or $y textbox on the form has no data, how could i also perform that check based on your revised code?

your assistance is appreciated! thanks!
Post Reply