douxie9347 2015-11-03 11:28
浏览 39
已采纳

在codeigniter中定义回调函数

I am trying to set error validation for strn and is needed to follow this strict rule,but as I am unaware of how to use callback functionality, can anybody help me in setting up the form validation for the same?

$this->form_validation->set_rules('strn', 'STRN', 'trim|callback_strn_check');

and the strict rules are:-

  • The STRN is a unique 15 digit number.

  • 1st ten digits are PAN number of the Assessee

  • 11th & 12th is the Service Tax Code (Its is either ST or SD)

One more important point is that this field is not mandatory, yet if user fills it inappropriately, then he should receive a form error.

  • 写回答

2条回答 默认 最新

  • dsdtumf776629385 2015-11-03 12:00
    关注

    try this,

    $this->form_validation->set_rules('strn', 'STRN','trim|max_length[15]|min_length[15]callback_strn_check');
    
    
    function strn_check($num){
        if(strlen($num) != 15){
            return false;
        } else if(substr($num, -2) == "SD" || substr($num, -2) == "ST") {
            $ten = substr($num, 0, 10);
            if(ctype_digit($ten))
                return true;
            return false;
        } else {
            return false;
        }
    }
    

    EDIT

    For the first 10 characters to be same as pan you can do like this,

    function strn_check($num){
        if(strlen($num) != 15){
            return false;
        } else if(substr($num, -2) == "SD" || substr($num, -2) == "ST") {
            $ten = substr($num, 0, 10);
            if($this->input->post("pan") == $ten)
                return true;
            return false;
        } else {
            return false;
        }
    }
    

    If you want you can use strtolower(), if case doesnt have any influence in validation. As of now, it will work only for uppercase letters(last 2 characters).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?