2012-12-29 59 views
2

這裏我是enens稅務字段的百分比,但是當我的enetr值是2.5,0.5而不是整數時,它會產生錯誤.. 這裏是我的驗證碼,任何想法輸入浮點數 float驗證codeigniter的號碼

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> 
+0

任何事情,任何提示將工作 –

+1

嘗試從驗證規則中刪除'is_natural' – Moes

+0

@Moes你可以看到我已經把is_natural驗證不允許負數,所以即時得到錯誤,你只能輸入possitve數字 –

回答

2

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函數。

希望它有幫助!

+0

謝謝男人我會嘗試這個,並很快回來 –

3

你可以試試這個:

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; 
    } 
} 

希望這個代碼將會幫助你.... :)

+0

謝謝男人我會嘗試這個,並很快回來 –

12

從驗證規則刪除is_naturalgreater_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