在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.

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).

i tried the answer given above and fixed few bugs,now its working absolutely perfect. here is the code:-

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