Jump to content

Validation Function Undesired Results


lspiehler

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/222506-validation-function-undesired-results/
Share on other sites

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.

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);

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.