...

count "(" in string

Jt107
11-29-2010, 06:20 PM
I need to do an input text validation which include opening parenthesis and closing parenthesis, what I need to validate is the opening parenthesis match with closing parenthesis. Here is a sample of the entry text:

thisis(test(of(matching(parenthesis)and)if)working

which one closing parenthesis is missing. I would like to warn the user to correct it before submit.

When I use ListValueCount(form.myValue,"(")> gives me 0 accourance.
Not sure how could I count the "(" and ")" to compare. Please advice.

Thanks,
JT

Jt107
11-29-2010, 08:19 PM
use the following function to solved the problem:
<cffunction name="countOccurrences" access="public" returntype="numeric" required="true">
<cfargument name="string" type="string" required="True" />
<cfargument name="subString" type="string" required="True" />

<cfset VAR position = findNoCase(arguments.substring,arguments.string,1) />
<cfset VAR count = 0 />

<cfif len(trim(arguments.substring))>
<cfloop condition="position NEQ 0">
<cfset count = count + 1 />
<cfset position = findNoCase(arguments.substring,arguments.string, position + len(trim(arguments.substring))) />
</cfloop>
<cfelse>
<cfreturn 0 />
</cfif>

<cfreturn count />
</cffunction>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum