CodeIgniter Forums
Validation class: alpha-dash-comma - ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Validation class: alpha-dash-comma - ? (/showthread.php?tid=4871)



Validation class: alpha-dash-comma - ? - El Forum - 12-20-2007

[eluser]tinawina[/eluser]
Hi -

I have a tag class that runs our site's tagging utility. I want to allow people to add tags that include alphanumeric characters, dashes and underscores which the validation class already let's me do easily via "alpha-dash". We are separating tags (which can be phrases, etc.) with a comma. So I want submissions to include not only alphanumeric characters, dashes, and underscores but also commas.

I went into the validation library to see if there was an easy way to do this. Here's the validation library method for alpha-dash:

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

Of course, I was hoping this would be as easy as just adding a related error message in the validation_lang file, and then adding a new method to the validation library like so:

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

But I get this error message:

Quote:"Message: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 13"

Anybody have any clues re: why I'm getting this error and what I need to do to make this change? Any help - much appreciated!


Validation class: alpha-dash-comma - ? - El Forum - 12-20-2007

[eluser]tonanbarbarian[/eluser]
try slashing the comma and see if that helps

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



Validation class: alpha-dash-comma - ? - El Forum - 12-20-2007

[eluser]ejangi[/eluser]
This works for me:
Code:
function alpha_dash_comma($str)
{
    return ( ! preg_match("/^([-a-z0-9_,])+$/i", $str)) ? FALSE : TRUE;
}

I took out the second "-" [Read: Dash] which preg recognises as a range-operator, except you weren't using it in that context. I'm not sure why the CI function has two lots of dashes - bug???


Validation class: alpha-dash-comma - ? - El Forum - 12-20-2007

[eluser]tinawina[/eluser]
Hey thanks!

ucantblamem -- your solution did the trick. I had tried a slash before the comma before to no avail. But thanks for the suggestion tonanbarbarian.

I also had to add a space in there since we are allowing people to enter phrases or a couple of words separated by a space. All is working well now. Here's where we landed:

Code:
function alpha_dash_comma($str)
{
    return ( ! preg_match("/^([-a-z0-9_, ])+$/i", $str)) ? FALSE : TRUE;
}
Thanks for the assistance!