1

i want to set form validation so that user cannot add special characters like $, @, &, etc... only alpha numeric characters are allow

$this->form_validation->set_rules('cat_name', 'Category Name', 'trim|xss_clean|required|is_unique[cht_category.cat_name]|min_length[5]|max_length[75]|alpha_numeric');

when i add category name with space then i get following error "The Category Name field may only contain alpha-numeric characters." when i add category name without space or single word then its working fine

2 Answers 2

4

Use:

alpha_numeric_spaces

Ref: https://www.codeigniter.com/userguide3/libraries/form_validation.html

Sign up to request clarification or add additional context in comments.

Comments

1

Well, alpha_numeric won't allow spaces to pass. Instead of using alpha_numeric, you can write your own function and pass it there (Codeigniter form validation. Alpha and spaces):

function alpha_dash_space($str)
{
    return ( ! preg_match("/^([-a-z_ ])+$/i", $str)) ? FALSE : TRUE;
} 

and then

$this->form_validation->set_rules('cat_name', 'Category Name', 'trim|xss_clean|required|is_unique[cht_category.cat_name]|min_length[5]|max_length[75]|callback__alpha_dash_space');

1 Comment

i have tried this but it will add special charaters like business types $ now i dont want to add these kinds of characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.