2

I'd like to have input validated with form_validation class, that'll allow me to put numeric or empty value in the field.
Something like this:

$this->form_validation->set_rules('field[]','The field','numeric or empty|xss_clean');

Is this possible to achieve?

5 Answers 5

8
$this->form_validation->set_rules('field[]', 'The field', 'numeric|xss_clean');

This should be sufficient in theory, since the field hasn't been set to required.

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

1 Comment

But, it returns me the errors. Might it be an Codeigniter's bug? I'm using 2.0.3
3

But, it returns me the errors

Then perhaps an extra step:

if (!empty($this->input->post('field[]')))
{
    $this->form_validation->set_rules('field[]', 'The field', 'numeric|xss_clean');
}

1 Comment

@sunpietro -- Have you had a chance to test it out yet?
1

For phone number with 10 digits:

 $this->form_validation->set_rules('mobile', 'Mobile Number ', 'required|regex_match[/^[0-9]{10}$/]'); //{10} for 10 digits number

Comments

0

I think you mean "Using Arrays as Field Names"

You can have a look at this page

Comments

0
$this->form_validation->set_rules('field[]','The field','is_natural|trim|xss_clean');
if ($this->form_validation->run() == FALSE) {
        $errors = $this->form_validation->error_array();
        if (!empty($errors['field'])) {
            $errors_data = $errors['field'];
        }
        print_r($errors_data);exit;
    }

You can use is_natural that Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc. For empty checking required rule is used that Returns FALSE if the form element is empty. So remove the required rule is if used. And use form_validation rules for showing your rules message

Comments

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.