Reputation: 6656
I tried setting a rule in codeigniter form validation to accept input such as numbers and dashes. I read the Codeigniter documentation but it doesn't support such thing. Is there any way to write a custom function? Any help would be much appreciated.
$this->form_validation->set_rules('phone', 'Contact No.', 'numeric');
Upvotes: 0
Views: 2903
Reputation: 6656
Now this works for me. Maybe it could help others.
// Allow dashes to numbers
function numeric_dash ($num) {
return ( ! preg_match("/^([0-9-\s])+$/D", $num)) ? FALSE : TRUE;
}
Upvotes: 0
Reputation: 4265
Within the form validation you can set a custom rule as a callback function (see this documentation section)
If you set your validation as
$this->form_validation->set_rules('phone', 'Contact No.', 'callback_my_function');
You can set up a function in your controller:
function my_function($data)
{
// your validation code
}
The $data parameter is passed through automatically from the field being validated, and you'll need to return true if it validates, or false if it doesn't for it to work.
Upvotes: 2