Jump to content

Fixing my statment


adam84

Recommended Posts

I am trying to only allow letters, hyphens, periods and spacings

 

Any suggestions would be helpful.

function checkName( $name ){
  		if(!eregi("[a-zA-Z.' ]+", $name)) 
    			return false;
  		return true;
}

 

Thank you

Link to comment
Share on other sites

@sasa:

 

sure, checking for presence of characters NOT acceptable is an alternate way to go.  However, it looks like you just c/p'd his base pattern, which doesn't match what he actually says he wants to match for.  He says hyphens are acceptable, and he did not mention single quotes.

 

@adam84:

 

FYI neither of these addresses a couple of issues that are commonly addressed with. 

 

- These regexes do not account for length limits.  Most sites want a name to have a minimum amount of characters, or at least limit the maximum.

- These regexes do not prevent values like "---------" or "              " or "- - - - - -" or ".....---.--  ---" or...you get the picture.

Link to comment
Share on other sites

Just an FYI. If you need a function that is supposed to return a Boolean then it is not necessary to do something such as

if($foo==$bar)
{
    return true;
}
else
{
    return false;
}

 

Just return the comparison. Using Pikachu's solution as an example:

function checkName($name) 
{
    return (ctype_alpha( str_replace(array('-', ' ', '.'), '', $name)) );
}

 

Link to comment
Share on other sites

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.