我在这里输入税收字段,但当我输入2.5,0.5等值而不是整数时,它会产生错误。这是我的代码验证,输入浮点数的任何想法
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>
将is_natural
从验证规则中删除并替换为greater_than[0]
和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]
将应用于numeric
来自codeigniter文档:
is_natural如果表单元素包含非自然数:0、1、2、3等,则返回FALSE
显然,像2.5、0.5这样的值不是自然数,因此它们将无法通过验证。您可以使用回调并在使用floatval()
PHP函数解析值后返回值。
希望有帮助!
你可以试试:
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;
}
}
希望这段代码能帮助到你....:)