php中的输入验证
我已经在codeigniter执行验证中完成了这项工作。如何在php本机中执行类似的工作?我的意思是验证您可以通过php变量访问发布的表单值。然后,您需要用php编写执行不同验证的函数:-php中的输入验证,php,validation,Php,Validation,我已经在codeigniter执行验证中完成了这项工作。如何在php本机中执行类似的工作?我的意思是验证您可以通过php变量访问发布的表单值。然后,您需要用php编写执行不同验证的函数:- 必需:这很简单,只需检查值是否不是空字符串 修剪:有一个 有效电子邮件:您需要使用 这些应该会让你开始,在接下来的时间里或者一个月里 希望这有帮助 我过去的做法是为它构建对象。。。表单对象、表单字段对象和表单字段验证程序对象 因此,您需要创建所有字段对象,如果需要,将验证器附加到这些对象上,然后将整个批次
- 必需:这很简单,只需检查值是否不是空字符串
- 修剪:有一个
- 有效电子邮件:您需要使用
希望这有帮助 我过去的做法是为它构建对象。。。表单对象、表单字段对象和表单字段验证程序对象 因此,您需要创建所有字段对象,如果需要,将验证器附加到这些对象上,然后将整个批次附加到表单上-因此您最终会得到如下结果:
public function registration()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');`enter code here`
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
}
new Validator(Validator::TYPE_EMAIL); //basic email validator
new Validator(Validator::TYPE_EMAIL_WITH_MX); //email validator with getmxrr()
new Validator(Validator::TYPE_REGEXP, '/^[\w]+$/'); //generic regular expression with the pattern to match as the second parameter
new Validator(Validator::TYPE_INT_MIN, 10); //integer with a minimum value of 10
new Validator(Validator::TYPE_REGEXP, '/^[\w\s]*$/', true); //the third parameter could be an override so that the validation is optional - if the field has a value it MUST validate, if it doesn't have a value, it's fine
验证器处理的事情包括确定字段数据是否是必需的,如果数据与空字符串(RegExp)匹配,那么它就不是必需的
但它们也可以处理电子邮件验证(有或没有getmxrr()查找)或其他任何事情,您只需为特定情况构建验证程序类型。。。或者您有通用验证器:
$oFieldUsername = new FormField('username', new Validator(Validator::TYPE_EMAIL));
$oFieldPassword = new FormField('password', new Validator(Validator::TYPE_PASSWORD));
$oForm = new Form(Form::METHOD_POST, '/path/to/action.php');
$oForm->attachField($oFieldUsername);
$oForm->attachField($oFieldPassword);
//form has not been posted
if(!$oForm->isReceived()) {
$oForm->render('/path/to/view.tpl.php');
}
//the form HAS been posted but IS NOT VALID
elseif(!$oForm->isValid()) {
$oForm->render('/path/to/view.tpl.php');
}
//the form HAS been posted and the data LOOKS valid
else {
//do processing and hand-off
}
这为您提供了验证所需的最大灵活性。Form::isValid()
方法所做的就是遍历所有附加的字段,检查它们是否有验证器,如果有,那么Validator::isValid()
方法是否返回true
您还可以将多个验证器附加到具有以下内容的字段:
public function registration()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email_address', 'Your Email', 'trim|required|valid_email');`enter code here`
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
}
new Validator(Validator::TYPE_EMAIL); //basic email validator
new Validator(Validator::TYPE_EMAIL_WITH_MX); //email validator with getmxrr()
new Validator(Validator::TYPE_REGEXP, '/^[\w]+$/'); //generic regular expression with the pattern to match as the second parameter
new Validator(Validator::TYPE_INT_MIN, 10); //integer with a minimum value of 10
new Validator(Validator::TYPE_REGEXP, '/^[\w\s]*$/', true); //the third parameter could be an override so that the validation is optional - if the field has a value it MUST validate, if it doesn't have a value, it's fine
。。。无论如何,我就是这样做的。您可以查看form_验证库来了解,CodeIgniter是如何使用代码实现这一点的,我不确定您希望在“php本机”中找到什么。php中没有表单(您可以创建一个生成HTML表单的php文件,但没有表示表单afaik的本机对象)。