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
How to allow empty or numeric values in codeigniter's form_validation class?
提问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