【问题标题】:Php Validation inside if statementif 语句中的 PHP 验证
【发布时间】:2012-05-06 19:52:24
【问题描述】:

我想知道以下 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]。

非常感谢。

马文·M

【问题讨论】:

  • $this-&gt;request-&gt;post['cnpj'] 的期望值是多少?整数还是字符串?
  • cnpj 将是一个字符串@LawrenceCherone
  • @Chandresh... 您的评论与我的问题有关,因为...
  • .... 因为如果您不接受他们的回答,人们不会愿意提供帮助。这就是为什么。
  • 25% 接受是不可接受的。如果海报给他们积分,人们更有可能提供帮助。这对每个人来说都是一场游戏。

标签: php validation radio-button


【解决方案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结构都遵循这种方式。

【讨论】:

  • 感谢您的回复,会看看的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多