php 如何在 codeigniter 的 form_validation 类中允许空值或数值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8371129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:33:20  来源:igfitidea点击:

How to allow empty or numeric values in codeigniter's form_validation class?

phpcodeignitervalidation

提问by sunpietro

I'd like to have input validated with form_validation class, that'll allow me to put numeric or empty value in the field.
Something like this:

我想用 form_validation 类验证输入,这将允许我在字段中输入数字或空值。
像这样的东西:

$this->form_validation->set_rules('field[]','The field','numeric or empty|xss_clean');

Is this possible to achieve?

这有可能实现吗?

回答by Sam Granger

$this->form_validation->set_rules('field[]', 'The field', 'numeric|xss_clean');

This should be sufficient in theory, since the field hasn't been set to required.

这在理论上应该足够了,因为该字段尚未设置为必需。

回答by stealthyninja

But, it returns me the errors

但是,它返回给我错误

Then perhaps an extra step:

然后也许是一个额外的步骤:

if (!empty($this->input->post('field[]')))
{
    $this->form_validation->set_rules('field[]', 'The field', 'numeric|xss_clean');
}

回答by Ashish pathak

For phone number with 10 digits:

对于 10 位数字的电话号码:

 $this->form_validation->set_rules('mobile', 'Mobile Number ', 'required|regex_match[/^[0-9]{10}$/]'); //{10} for 10 digits number

回答by Ogulcan Orhan

I think you mean "Using Arrays as Field Names"

我想你的意思是“使用数组作为字段名称

You can have a look at this page

你可以看看这个页面