[英]float Number validation check for codeigniter
Here I am entering tax field in % but when i enter values like 2.5,0.5 other than integer it is generating error.在这里,我以 % 输入税收字段,但是当我输入 2.5,0.5 之类的值而不是整数时,它会产生错误。 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>
remove the is_natural
from the validation rules and replace it with greater_than[0]
and less_than[100]
从验证规则中删除is_natural
并将其替换为less_than[100]
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]
will apply numeric
greater_than[0]
将应用numeric
From the codeigniter documentation:来自 codeigniter 文档:
is_natural Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc. source is_natural 如果表单元素包含自然数以外的任何内容,则返回 FALSE:0、1、2、3 等。 source
Clearly, values like 2.5,0.5 are not natural numbers so they will fail validation.显然,像 2.5,0.5 这样的值不是自然数,因此它们将无法通过验证。 You can use a callback and return the value after parsing the value with floatval()
PHP function.您可以使用回调并在使用floatval()
PHP 函数解析值后返回值。
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.... :)希望这段代码能帮助你.... :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.