2012-04-23 35 views
0

我有一個用戶名字段,我不想接受像{*)&(%^%()_}這樣的字符。我需要添加到我的表單驗證規則中?如何防止使用Codeigniter表單驗證的特殊字符?

$this->form_validation->set_rules('user_name' 'User Name', 'trim|required|min_length[4]|xss_clean'); 
+1

毫無疑問,在這裏...請嘗試重組,並問一個問題。 – Mischa 2012-04-23 05:12:44

+0

...並且不要忘記解釋你已經嘗試了什麼以及你卡在哪裏。 – 2012-04-23 05:13:25

+0

限制在用戶名中使用特殊字符,即使用codeigniter框架。 $ this-> form_validation-> set_rules('user_name','用戶名','trim | required | min_length [4] | xss_clean');我已經給出了一些用戶名的規定,現在我想在用戶名字段中設置限制,並希望在上面的代碼行中添加更多的代碼。 im卡在這裏 – 2012-04-23 05:21:34

回答

4

只看了doc。這並不難。

如果要將用戶名字符限制爲字母和數字,請使用alpha_numeric規則。

$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean|alpha_numeric'); 

,但如果你想限制只能由字母,使用alpha規則。或者,如果您想將用戶名限制爲字母,數字,下劃線和破折號,請使用alpha_dash規則。

+0

非常感謝@kemal它真的非常有幫助它的工作 – 2012-04-23 05:46:53

+0

@ArunKumar,請點擊「V」在這個答案旁邊接受它。這樣做有兩點:它向其他用戶顯示該問題具有滿意的答案(不需要新的答案)。對於凱末爾的努力,它以[聲譽](http://stackoverflow.com/faq#reputation)的形式表示讚賞。 – Mischa 2012-04-23 09:20:51

+0

@Mischa ok im doing it – 2012-04-23 12:03:11

0

你應該使用回調函數來檢查,如果用戶名與模式匹配:

$this->form_validation->set_rules('username', 'Username', 'username_check'); 
.... 
public function username_check($str) 
{ 
    if ($str == 'test') 
    { 
     $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"'); 
     return FALSE; 
    } 
    else 
    { 
     return TRUE; 
    } 
}