Jump to content

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.

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

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.