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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching problems using eregi 1

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
CA
Hi,

The following code currently accepts whatever the value of the variable is, as long as it contains a letter. It will reject "9" but will accept "a9". I need to allow only letters - am I using this properly?

Thanks.

Code:
$patternAlpha = "[a-z]";
if(!eregi($patternAlpha,$namef)){
			$errorFlag = 1;
			$errorMsg .= "The First Name field may contain only letters.<br>";
		}

frozenpeas
 
First of all, I would recommend that you use PCRE regular expressions.
There are a few things you need to do:
1. Patterns should have pattern delimiters, usually people use forward slashes, e.g.
Code:
$patternAlpha = "/[a-z]/"l
2. Your logic is a bit flawed. Your epressions says basically if there is any alpha character in the string, it passes the test. You need to extend the definition and tighten the pattern:
Code:
$pattern = "/^[a-z]+$/i";
if (!preg_match($pattern, $namef)){
   $errorFlag = true;
   $errorMsg .= "Blah blah blah.";
}
Explanation of the pattern:
/ -> initial pattern delimiter
^ -> start of the subject (the string to be examined)
[ -> character class definition start
a-z -> any alpha from a to z
] -> character class definition end
+ -> one or more (excludes empty strings)
$ -> end of the subject
/ -> end pattern delimiter
i -> be case insensitive, covers a-z and A-Z
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top