Welcome Guest, Not a member yet? Register   Sign In
editing post data before form_validation is run on it?
#1

[eluser]hamobi[/eluser]
I am running this validation on a form field for a phone number

Code:
$this->form_validation->set_rules('phone', 'Phone', 'trim|required|is_natural|exact_length[10]|xss_clean');

the problem is that the user is not able to enter dashes or parentheses. So I have been trying to strip those things before the data is ran through the validation function:

Code:
$phone = $this->input->post('phone');
$this->form_validation->set_rules($phone, 'Phone', 'trim|required|is_natural|exact_length[10]|xss_clean');

however, this it is now not validating the data at all. How am I able to make this happen?

thank you.
#2

[eluser]Matalina[/eluser]
use a call back function and preg_match if parenthesis and dashes are there.


add callback_function_name to your list of validation

#3

[eluser]hamobi[/eluser]
[quote author="Matalina" date="1330973304"]use a call back function and preg_match if parenthesis and dashes are there.


add callback_function_name to your list of validation

[/quote]

i want to remove all dashes and parantheses. could a callback function do this as well? could you post a code sample please?
#4

[eluser]Kamarg[/eluser]
Code:
<?php
// This line just has to come before you run the form validation. It doesn't matter if it is before or after you call set rules
$_POST['phone'] = str_replace(array('(', ')', '-'), '', $_POST['phone']);
$this->form_validation->set_rules('phone', 'Phone', 'trim|required|is_natural|exact_length[10]|xss_clean');
if(!$this->form_validation->run())
{
  // Validation failed
}
else
{
  // Validation succeeded
}
?>
#5

[eluser]CroNiX[/eluser]
@Kamarg, you might want to check to see if isset($_POST['phone']) first, or that will cause an error if the form hasn't been submitted yet.
#6

[eluser]Kamarg[/eluser]
[quote author="CroNiX" date="1330976741"]@Kamarg, you might want to check to see if isset($_POST['phone']) first, or that will cause an error if the form hasn't been submitted yet.[/quote]

You are correct. That was really just there to be an example of how to edit the data. Probably should've included a note mentioning that all the normal precautions should be taken.




Theme © iAndrew 2016 - Forum software by © MyBB