I want to check if a string is valid or invalid using CodeIgniter's Form Validation library.
If the input string has 9 numbers + 1 character in this set: [xXvV]
or the string is empty, it is considered as valid. (e.g.: "123456789X", "987654321v", ""). I implemented a callback rule using preg_match()
to no avail.
Setting rules in my controller:
$this->form_validation->set_rules('nic', 'NIC', 'trim|xss_clean|callback_checkNIC');
And here's my callback function:
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->form_validation->set_message('checkNIC', 'The NIC is not valid');
return FALSE;
}
}
Any ideas?
Add this method to your controller class where you're doing the validation. Or if you want to put it somewhere else, make sure it's callable:
public function checkNIC($nic)
{
$this->form_validation->set_message('checkNIC', 'The NIC is not valid');
return $nic === '' || (bool) preg_match('/^\d{9}[xXvV]$/', $nic);
}
Have a look:
https://3v4l.org/X3DhC
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments