使用Codeigniter表单验证库,如何确保至少输入一个字母? [英] Using the Codeigniter form validation library, how can I ensure at least one letter is entered?
问题描述
我正在使用Codeigniter表单验证.在我的注册表格中, Username
字段仅允许使用 123456 之类的数字.我不希望这种情况发生.
I am using Codeigniter form validation. In my registration form the Username
field allows only numbers like 123456. I don't want this to happen.
我的验证规则如下
'rules'=>'trim|required|alpha_numeric|min_length[6]|xss_clean'
我想防止用户仅输入数字字符串.字母数字字符串很好,字母字符串很好,但是纯数字字符串却不好.
I want to prevent users entering just numeric strings. Alpha numeric strings are fine, alpha strings are fine, but purely numeric ones are not.
推荐答案
仅允许字母
在规则中添加 alpha
,并从规则中删除 alpha_numeric
Add alpha
to your rules and remove alpha_numeric
from your rules
您可以使用此页面作为内置验证的参考点规则.
You can use this page as a point of reference for built in validation rules.
自从您澄清之后.
要实现此目的,没有内置的验证规则.您将需要通过创建 libraries/MY_Form_validation.php
文件来扩展 Form_validation
库.有关如何扩展库的信息,请参见本手册页面.
To achieve this, there's no built in validation rule. You will need to extend the Form_validation
library by creating a libraries/MY_Form_validation.php
file. See this manual page on how to extend libraries.
在此文件中,创建以下功能
In this file, create the following function
function at_least_one_letter($string) {
return preg_match('#[a-zA-Z]#', $string);
}
然后,您可以将验证规则 at_least_one_letter
添加到您的规则中.
Then you can add the validation rule at_least_one_letter
to your rules.
这篇关于使用Codeigniter表单验证库,如何确保至少输入一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!