Welcome Guest, Not a member yet? Register   Sign In
why is form validation having issue with spaces?
#1

[eluser]scout[/eluser]
I have a codeigniter installation in which the presence of spaces in form fields fails validation for alpha or alpha-dash.

Code:
$this->form_validation->set_rules('first_name', 'First Name', 'alpha_dash|trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'alpha_dash|trim|required');
$this->form_validation->set_rules('title', 'Title', 'alpha_dash|trim|required');
$this->form_validation->set_rules('company', 'Company', 'alpha_dash|trim|required');

Any quick ideas as to why?

This used to work just fine and now alpha or alpha-dash validation won't pass if there is any spaces in the post. AFAIK, spaces should pass validation and it used to work. I went back and checked previous submissions.

I have a nearly identical contact form running on another server and spaces are not a problem. The validation library on both servers is identical. I modified it on the problem server to specifically allow the space character and it still fails. I've done three contact forms in CI now on three different servers and didn't have this problem until this popped up after the fact. I reported back the POST submission and did not observe any spaces spoiled by encoding or other detritus.

I am running CI v. 1.72 on both sites. Running php 5.3.2 on problem server.

Can someone please confirm how spaces are supposed to be validated with alpha or alpha-dash and why this might have suddenly stopped working? Thanks!
#2

[eluser]scout[/eluser]
Is it possible this problem is caused by a server configuration change than a codeigniter problem? Looking for some ideas, please.
#3

[eluser]Unknown[/eluser]
This problem still exists in CI 2.0.3 but you can use a custom RegEx to validate your names:


Code:
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|callback_validate_name');

function validate_name($str) {
        if (preg_match("/^[A-Z][a-zA-Z -]+$/", $str) !== 0) {
            return true;
        } else {
            $this->form_validation->set_message("validate_name", '%s is not valid.');
            return false;
        }
}

This RegEx expression will ensure that first_name contains letters, dashes and spaces only, must start with upper case letter and must not start with a dash. (source: PHP validation and verification)

Hope this helps.
#4

[eluser]scout[/eluser]
I ended up hacking the system form_validation library by adding \s to the alpha_dash function. All the tests I ran against the server wouldn't reveal any server or configuration problems. This hack resolved the matter instantly. Still, on another server I did not have this problem. Weird.

Code:
function alpha_dash($str)
{
  return ( ! preg_match("/^([-a-z0-9_-\s])+$/i", $str)) ? FALSE : TRUE;
}
#5

[eluser]Samus[/eluser]
[quote author="scout" date="1327269999"]I ended up hacking the system form_validation library by adding \s to the alpha_dash function. All the tests I ran against the server wouldn't reveal any server or configuration problems. This hack resolved the matter instantly. Still, on another server I did not have this problem. Weird.

Code:
function alpha_dash($str)
{
  return ( ! preg_match("/^([-a-z0-9_-\s])+$/i", $str)) ? FALSE : TRUE;
}
[/quote]
Same thing happened to me bro. All my previous projects never had this problem, both locally and online. And now spaces aren't going through validation.

Thanks for the snippet anyway..
#6

[eluser]scout[/eluser]
Thanks for the note, Samus. I thought I was going crazy.

Well, I am going crazy but at least I know I am not alone!
#7

[eluser]CroNiX[/eluser]
[quote author="scout" date="1327269999"]I ended up hacking the system form_validation library by adding \s to the alpha_dash function. All the tests I ran against the server wouldn't reveal any server or configuration problems. This hack resolved the matter instantly. Still, on another server I did not have this problem. Weird.
[/quote]It would be much better to extend the validation class with your fix rather than alter CI code. It will make upgrading MUCH easier. If you don't, you will have to go back and manually reapply any "fixes" or alterations you did....and if you don't track them all...you're not going to have much fun.
#8

[eluser]scout[/eluser]
[quote author="CroNiX" date="1328990227"]It would be much better to extend the validation class with your fix rather than alter CI code. It will make upgrading MUCH easier. If you don't, you will have to go back and manually reapply any "fixes" or alterations you did....and if you don't track them all...you're not going to have much fun.[/quote]

True that.




Theme © iAndrew 2016 - Forum software by © MyBB