Welcome Guest, Not a member yet? Register   Sign In
form validation regular expressions sorry
#1

[eluser]tim1965[/eluser]
Hi

I am trying to add a custom form validation function using MY_Form_validation. I have the function working and generating error messages on validation. However the regular expression i have seems not to be be doing what i want it to do.
Essentially i want a regular expression that allows all the normal English language characters people use when writing.
So a-z 0-9 " \ / ( ) ´ : _ - + = . , !
and a space
Really just to valiadate standard text input for text areas.

Currently i have
Code:
preg_match("/^([-a-z0-9\_\:\´\/\.\,\(\)\+\"-\s])+$/i", $str)

But this is disallowing ´ and i am not sure why.
I find it very strange that CI´s standard validation doesnt cater for this as most people woukd have had to extend this to cater for normal textfield input. So if any kind soul has something they can post to address the above then that would be fantastic.
I know i need to get my head round these at some point, i am just not ready yet.
Thanks in advance.
#2

[eluser]pistolPete[/eluser]
Do you mix up an apostrophe ( ' ) and an accent? ( ´ ) ?
#3

[eluser]tim1965[/eluser]
No i have checked that.

I cannot believe the standard alpha_dash doesnt even allow spaces. So none of the CI default validation regex matching will work for a standard text field with a space in it.
I can only think that everybody has to modify this just to handle spaces in text.
I cannot believe i am the first to bump into this.
Thanks for responding though.
#4

[eluser]CroNiX[/eluser]
here is what I use for that in MY_Form_validation:
Code:
function name($str)
{
    $this->set_message('name', 'The %s field may only contain alpha-numeric characters, spaces, underscores and dashes.');
    return ( ! preg_match("/^([-a-z0-9_ -])+$/i", $str)) ? FALSE : TRUE;
}

another useful one for address type fields:
Code:
function address($str)
{
    $this->set_message('address', 'The %s field may only contain alpha-numeric characters, spaces, underscores, periods, apostrophies and dashes.');
    return ( ! preg_match("/^([-a-z0-9_. -#'])+$/i", $str)) ? FALSE : TRUE;
}
#5

[eluser]tim1965[/eluser]
Thanks

I tried adding ´ to yours, but the form failed validation again whne i try and use ´in a field.
My expression works correctly for everything bar ´and idont know why.
Thanks for your responses i will keep on testing.
#6

[eluser]tim1965[/eluser]
This is what i ended up going with after a lot of testing
Code:
return ( ! preg_match("/^([-a-z0-9\_\:\\\\\/\.\,\(\)\+\"-\s'])+$/i", $str)) ? FALSE : TRUE;
Many thanks for all your help.




Theme © iAndrew 2016 - Forum software by © MyBB