Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help With eregi

Status
Not open for further replies.
Aug 27, 2001
502
US
I would like to use eregi to validate a form. A person's name can include a-z, spaces, & hyphens and contain 2 or more letters. So I have:

Code:
eregi('^[a-z\ -]{2,}$', $fname);

It's not working the way I want though. If a user enters two hyphens or two spaces, it says it's valid. I need to modify the regular expression so that there is 2 or more letters. The input can also contain hyphens or spaces, but they should not be counted toward the 2 or more letters.

Any help is appreciated.

Thanks,
-Ron

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
I don't think this is something that can be done in a single regular expression. At the very least, it's not something that can be done efficiently. Counting the number of letters that appear in a string is not something regular expression engines were designed to do.

I recommend that you use your current regex to make sure there are no illegal characters, but use a second step to insure there are two letters. Maybe something like preg_grep() with a pattern of a single letter. If the array returned has at least two elements, you know two letters exist in the string.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Here's what I ended up doing. It seems to work. Do you see anything wrong with it?

Code:
function validateName($name){
	if (eregi("^[a-zA-Z\ '-]{2,}$", trim($name, " '-"))) {
   	return true;
   }else{
   	return false;
   }
}

-Ron

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
No, that's actually pretty slick.

Thanks. That means a lot coming from you.

If I must find fault (and trust me, I must), it would be that since you're using eregi(), you don't need both "a-z" and "A-Z" in the expression.

Good point. And for that matter, I probably don't need "
Code:
\ '-
" in eregi() since I'm trimming those three characters out.

Thanks,
Ron

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
Actually, you do need the other characters in the regex. Though you probably don't have to escape the space character.

Assume that the user enters as a username the string "-- A---A --".

The trim() function will remove the non-letter charters only from the beginning and end of the string, so that the string looks like "A---A".

If your case-insensitive regex is '[a-z]{2,}', the script will not accept the input because that regular expression requires that the letters be next to one another.

The requirement that the characters be next to one another wasn't in your original specification. Should it have been?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
No. I thought trim() removed the specified characters from the whole string, not just the ends (like it does in VB).

I'll just leave it alone then (except for the A-Z). If it aint broke, don't fix it.

Thanks again.
-Ron

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
No, the VB function just does the ends. I thought that I had read somewhere the the PHP function does the middle too.

Thanks for your help.

-Ron

-We are all given the same deck of cards, it's how we play the hand we are dealt which makes us who we are.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top