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!

Regular Expression help 2

Status
Not open for further replies.

prrm333

Programmer
Apr 14, 2003
97
US
I have been trying to make a regular expression for a last name field. The parameters are as follows: The only allowable characters are letters (upper and lower case), a period (.) and a dash (-). In using the ReFind command I had tried: ReFind("[a-zA-Z/-/.]", string but no luck so far. Any suggestions?
 
Are you trying to verify that only the allowable characters are there, or are you trying to strip out everything that's not allowable?



Hope This Helps!

Ecobb
Beer Consumption Analyst

"My work is a game, a very serious game." - M.C. Escher
 
what you did there would evaluate to true as long as you have one of those characters so "M*((*&%^()" would evaluate to true because of the M. you want to look for invalid characters.
ReFind("[^a-zA-Z/-/.]", string)

that says find anything that is NOT a letter - or . so

"M*((*&%^()" would return 2

<cfif refind("[^a-zA-Z/-/.]", string) gt 0>
<cfset error = "You can't do that, block head">
</cfif>

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
when i said
bombboy said:
so "M*((*&%^()" would evaluate to true
I ment that it would return 1 because "M" is in the first position of the string. it doesn't actually return "true" sorry for the confusion.

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
I ended up using the following to get it to work:

<cfif not REFind("[a-zA-Z]*[\.]*[\-]*", string) or
REFind("[[:space:],[:digit:]]", string) or
REFind("[~`!@##\$%\^\&&\*\(\)_\+=\:\;\""\''<<>>\?,/\\{}]", string) or
REFind("\[", string) or REFind("\]", string)>

**** Bad String! ****
<cfelse>
**** Good String! ****
</cfif>

Thanks
 
I think you'll find this regex pretty slick. It will find ANYTHING that is not on the approved list...
Code:
<!--- Regex tests --->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" >
 <head>
  <title>Regex Test</title>
 </head>
 <body>

<cfset testList = "JoeSchmoe,Joe Schmoe,joe@schmoe.com,Joe-Schmoe.yo,JoeSchmoe9">
<cfloop list="#testList#" index="testString">
	<cfif REFind("[^a-zA-Z\.\-]", testString)>
		<cfoutput><p>#testString# is BAD.</p></cfoutput>
	<cfelse>
		<cfoutput><p>#testString# is GOOD.</p></cfoutput>
	</cfif>
</cfloop>

 </body>
</html>

The trick to this is to use the negation (^) operator. This regex will find only the first occurrence of the bad character and then exit. This makes it very efficient. Some good reference for regexes are:
 
Hmm... looking back, I see that bombboy already gave the answer:
bombboy said:
<cfif refind("[^a-zA-Z/-/.]", string) gt 0>
<cfset error = "You can't do that, block head">
</cfif>
He simply had put forward slashes (/) instead of backslashes (\). Good job bombboy! I gave you a star. [2thumbsup]
 
oh crap i did didn't I? I just copied the existing expression and added the ^ should have paid more attention.

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
so according to the regexp this is completely valid

....---MY-name---HE-..-rE...--


prrm333 : Do you have any other constraint in the field? Can they enter as many periods or dashes as they want?

If you don't have something already defined, then I think you should come up with some kind of standard.




grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
he's probably right. I'm sure you'll never get someone with a name like that... or a big enough turd to do something like that but maybe limit it to one or two so you can still get names like "smith-jones .jr" ... if you're a guy with a hyphenated name :)

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top