CodeIgniter Forums

Full Version: how to remove special characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Wondering Coder[/eluser]
hello to everyone

just would like to ask how to remove this: -, [], / or any special characters in my input

tried setting rules in validation using xss_clean but it still it save those character.

e.g input:
1111-1-1111 = must only save 111111111
2222/2/2222 = must only save 222222222
2222#222#22 = must only save 222222222


El Forum

Use a callback on the input field. Then in the callback function use a regex.

El Forum

sounds good to me... but make sure you do all your other validation before you make your call back. i.e.

$this->form_validation->set_rules('phoneNumber', 'Phone Number', 'trim|required|min_length[8]|myCustomCallBack

As regex can be quiet taxing in processing power/time. Get your input as close to validated before applying your regex to save a little overhead. It may seem trivial now, but it's a good habit to get into, especially for larger applications and complex regex stuff