如何在Codeigniter中验证地址字段 [英] How to validate address field in Codeigniter
问题描述
我正在尝试将一些验证规则应用于CodeIgniter中的表单数据.
I am trying to apply some validation rules to my form data in CodeIgniter.
预期的允许的输出示例,例如: 22个社会,一些街道和城市.223399
Expected Allowed output example like this: 22-some society, some street, city. 223399
我输入的内容用于确认验证: 42号协会3号,一些街道.街机@ ## *
What I Entered for check the validation: 42-some Society-3, some street. arcade @##*
这是我用来验证地址的功能.
This is my function which I use to validate the address.
function addr_line1($addr_line1) {
if (preg_match('/^[a-z0-9 .\-]+$/i',$addr_line1) !== FALSE)
return TRUE;
$this->form_validation->set_message('addr_line1', 'allow only space,comma,dot,numbers and alphabets.');
return FALSE;
}
现在,我将所有验证都放入 config/form_validation.php
Now I put all my validation in the config/form_validation.php
array(
'field' => 'addr_line1',
'label' => 'Address Line One',
'rules' => 'required|max_length[100]|callback_addr_line1'
),
毕竟,我没有收到任何验证错误.我没有按照正确的程序进行吗?还是正则表达式代码应该用来验证这种类型的数据?
After all this,I didn't get any validation error. Am I not following the proper process? or what should the regex code to validate this type of data?
推荐答案
更改自
function addr_line1($addr_line1) {
if (preg_match('/^[a-z0-9 .\-]+$/i',$addr_line1) !== FALSE)
return TRUE;
$this->form_validation->set_message('addr_line1', 'allow only space,comma,dot,numbers and alphabets.');
return FALSE;
}
到
function addr_line1($addr_line1) {
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $addr_line1))
{
$this->form_validation->set_message('addr_line1', 'allow only space,comma,dot,numbers and alphabets.');
}else{
return true;
}
}
注意:-,您可以替换£$%& *()} {@#〜?><>,| = _ +¬-
和你不允许的角色
Note:- you can replace £$%&*()}{@#~?><>,|=_+¬-
with your disallowed character
这篇关于如何在Codeigniter中验证地址字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!