if语句中的Php验证

时间:2012-04-26 04:43:44

标签: php validation radio-button

我想知道以下php验证是否正确编码。它有效,但我想知道是否有更好的方法。

if(isset($_POST['account_type']) && $_POST['account_type']==2){

if ((strlen(utf8_decode($this->request->post['cnpj'])) < 3) || (strlen(utf8_decode($this->request->post['cnpj'])) > 32)) {
            $this->error['cnpj'] = $this->language->get('error_cnpj');
        }   
}

我需要这样,因为我有一个隐藏的必填字段,只显示给申请公司帐户的客户。单击单选按钮后会显示该字段。因此,当客户点击值为2的零售商帐户单选按钮时,我只需要验证该字段[cnpj]。

非常感谢。

Marvin M

1 个答案:

答案 0 :(得分:1)

您可以像下面的代码行一样执行代码:

if($_POST['account_type']==2 && $this->formValidation()){

       // Do your next process what you want to do if all set

}else{

     return $this->error;

}

//表单验证功能

function formValidation(){

if ((strlen(utf8_decode($this->request->post['cnpj'])) < 3) || (strlen(utf8_decode($this->request->post['cnpj'])) > 32)) {
            $this->error['cnpj'] = $this->language->get('error_cnpj');
        }   

}

我认为这将是在服务器端进行所有表单验证的一种更好的方法。 而且大多数MVC结构都遵循这种方式。