对codeigniter的float数字验证检查

Here I am enetring tax field in % but when i enetr values like 2.5,0.5 other than integer it is generating error.. Here is my code for Validation,any idea for entering float numbers

function _set_rules()
{
  $this->form_validation>set_rules('pst','PST','trim|required|is_natural|numeric|
   max_length[4]|callback_max_pst');
  $this->form_validation->set_rules('gst','GST','trim|required|is_natural|numeric|
max_length[4]|callback_max_gst');
}
function max_pst()
 {
   if($this->input->post('pst')>100)
    {
      $this->form_validation->set_message('max_pst',' %s Value Should be less than or equals to 100');
return FALSE;
    }
   return TRUE;
  }
function max_gst()
  {
    if($this->input->post('gst')>100)
      {
    $this->form_validation->set_message('max_gst',' %s Value Should be less than or equals to 100');
    return FALSE;
    }

   return TRUE;
  }
</code>

From the codeigniter documentation:

is_natural Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc. source

Clearly, values like 2.5,0.5 are not natural numbers so they will fail validation. You can use a callback and return the value after parsing the value with floatval() PHP function.

Hope it helps!

You can try this :

function _set_rules()
{
  $this->form_validation>set_rules('pst','PST','trim|required|
  numeric|max_length[4]|callback_max_pst');
  $this->form_validation->set_rules('gst','GST','trim|required|
  numeric|max_length[4]|callback_max_gst');
}

function max_pst($value) {
    $var = explode(".", $value);
    if (strpbrk($value, '-') && strlen($value) > 1) {
        $this->form_validation->set_message('max_pst', '%s accepts only 
        positive values');
        return false;
    }
    if ($var[1] > 99) {
        $this->form_validation->set_message('max_pst', 'Enter value in 
        proper format');
        return false;
    } else {
        return true;
    }
}

Hope this code will help you.... :)

remove the is_natural from the validation rules and replace it with greater_than[0] and less_than[100]

function _set_rules()
{
  $this->form_validation>set_rules('pst','PST','trim|required|
  greater_than[0]|less_than[100]|max_length[4]|callback_max_pst');
  $this->form_validation->set_rules('gst','GST','trim|required|
  greater_than[0]|less_than[100]|max_length[4]|callback_max_gst');
}

greater_than[0] will apply numeric