title: String validation in CodeIgniter using callback rules
body: <p>I want to check if a string is valid or invalid using CodeIgniter's Form Validation library.  </p>
  
  <p>If the input string has 9 numbers + 1 character in this set: <code>[xXvV]</code> or the string is empty, it is considered as valid. (e.g.: "123456789X", "987654321v", ""). I implemented a callback rule using <code>preg_match()</code> to no avail.  </p>
  
  <p>Setting rules in my controller:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$this-&gt;form_validation-&gt;set_rules('nic', 'NIC', 'trim|xss_clean|callback_checkNIC');
  </code></pre>
  
  <p>And here's my callback function:  </p>
  
  <pre class="lang-php prettyprint-override"><code>public function checkNIC($nic){
          $reg = '/^(\d\d\d\d\d\d\d\d\d)[xXvV]$/';
  
          if(preg_match($reg, $nic) || $nic == '') {
              return TRUE;
          }
          else {
              $this-&gt;form_validation-&gt;set_message('checkNIC', 'The NIC is not valid');
              return FALSE;
          }
      }
  </code></pre>
  
  <p>Any ideas? </p>