Page 1 of 1

Validate decimal places on a form with php

Posted: Tue Apr 26, 2011 6:41 pm
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.

Re: Validate decimal places on a form with php

Posted: Wed Apr 27, 2011 1:58 am
by andym01480
is_numeric() does return true for strings with decimal places

Re: Validate decimal places on a form with php

Posted: Wed Apr 27, 2011 8:08 am
by prensil
Is_numeric will definately work here.

Re: Validate decimal places on a form with php

Posted: Wed Apr 27, 2011 7:25 pm
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 
?>

Re: Validate decimal places on a form with php

Posted: Thu Apr 28, 2011 3:50 am
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

Re: Validate decimal places on a form with php

Posted: Thu Apr 28, 2011 9:25 am
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!