lspiehler Posted December 23, 2010 Share Posted December 23, 2010 I'm trying to make a function to validate business names, allowing numbers and letters only 3-40 characters, but It is still allowing the "&" symbol which will definitely cause problems. Here's the function: function fnValidateBusiness($business){ #alphabet only allowed. Minimum 3 characters, maximum 40. return preg_match('/^[a-zA-Z0-9 -\']{3,40}$/i', $business); } Any ideas? Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 23, 2010 Share Posted December 23, 2010 According to your description, no pattern is even needed. if( ctype_alnum($var) && strlen($var) > 2 && strlen($var) < 41 ) { //validation succeeded. } [/code] Quote Link to comment Share on other sites More sharing options...
lspiehler Posted December 23, 2010 Author Share Posted December 23, 2010 but will that allow my special characters? (- ') Quote Link to comment Share on other sites More sharing options...
Maq Posted December 23, 2010 Share Posted December 23, 2010 but will that allow my special characters? (- ') That's what the ctype_alnum check is for. It checks for alphanumerics. Quote Link to comment Share on other sites More sharing options...
lspiehler Posted December 23, 2010 Author Share Posted December 23, 2010 I understand that, but I'm not sure which one of us is misunderstanding the other. Those special characters that I need to allow, are not alphanumeric. So a string containing the characters I need to allow, using your validation, wouldn't allow me to use them. What I'm looking for is something that let's me use hyphens, single quotes, spaces and alphanumeric characters, which is what my function does, but my problem is that it also allows the "&" character for some reason, which is unacceptable. I feel like your not understanding me, but i am new at this, so maybe I'm just not grasping your concepts. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 24, 2010 Share Posted December 24, 2010 No, it will not, but that isn't what you initially said either: I'm trying to make a function to validate business names, allowing numbers and letters only 3-40 characters Quote Link to comment Share on other sites More sharing options...
DavidAM Posted December 24, 2010 Share Posted December 24, 2010 I think your problem is trying to include the hyphen in a character class. preg_match('/^[a-zA-Z0-9 -\']{3,40}$/i', $business); That dash you have is allowing SPACE through SINGLE-QUOTE. To use a literal dash in a character class, put it first or last or escape it: preg_match('/^[a-zA-Z0-9 \-\']{3,40}$/i', $business); Quote Link to comment Share on other sites More sharing options...
lspiehler Posted December 24, 2010 Author Share Posted December 24, 2010 Forgive me. I seem to have completely forgot to mention my "special characters" in my original post, but DavidAM was right! After having checked my function, I realized I'd gone back earlier and escaped the hyphen and just never re-checked it. Certainly not a waste because now I understand. Thanks for all the help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.