简体   繁体   English

Codeigniter验证规则帮助

[英]Codeigniter Validation Rules Help

I want to validate landphone and mobile phone fields in my CI application. 我想在我的CI应用程序中验证陆地电话和手机字段。 I have set validation rules as integer but user cannot enter values seperated by " - " . 我已将验证规则设置为整数,但用户无法输入以“-”分隔的值。 If i use "text" type, user can enter alphabets also.....how do i solve this issue....i want user to enter "-" and "+" values in the fields and not any other texts 如果我使用“文本”类型,则用户也可以输入字母.....我该如何解决此问题....我希望用户在字段中输入“-”和“ +”值,而不要输入其他任何文本

Code: 码:

$this->form_validation->set_rules('land_phone','Land Phone','trim|required|integer|min_length[1]|max_length[50]|xss_clean');
$this->form_validation->set_rules('mobile_phone','Mobile Phone','trim|required|integer|min_length[1]|max_length[50]|xss_clean');

You cannot enter a number with "-" since you defined integer as the validation rule. 由于您将integer定义为验证规则,因此不能使用“-”输入数字。 Therefore, the validation will fail. 因此,验证将失败。 You need to work with RegEx so that you can create more complex validation. 您需要使用RegEx,以便可以创建更复杂的验证。 See the topic on validations in the CI manual for more info. 有关更多信息,请参见CI手册中有关验证的主题。

Your validation rule: 您的验证规则:

$this->form_validation->set_rules('foo', 'Number', 'callback_number_validation');

Note how the keyword callback_ is used to identify CI your function for validation. 请注意,如何使用关键字callback_来标识CI以进行验证的功能。

Your callback function: 您的回调函数:

//$str will be the value you want to verify
function number_validation($str) {
    return preg_match("your_regex", $str) ? true: false;
}

Out of the box, I don't think there is anything that can do exactly that. 开箱即用,我认为没有什么可以做到这一点。 You'll probably need to use a callback function and check it with some regex. 您可能需要使用回调函数,并使用一些正则表达式对其进行检查。 I'm not too fluent with regex so I couldn't tell you exactly what you need there. 我不太会使用正则表达式,所以我无法确切告诉您那里需要什么。

You can find more about callbacks in the CodeIgniter User Guide: http://codeigniter.com/user_guide/libraries/validation.html 您可以在《 CodeIgniter用户指南》中找到有关回调的更多信息: http : //codeigniter.com/user_guide/libraries/validation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM