Download
Academy
Current location: Downcodes.com -> Academy -> Programming -> ASP tutorial -> Examining a text string with ASP and DHTML
Recommend
HOT TOP10
Examining a text string with ASP and DHTML
Date: 2008-1-4 Author: Hit: View:[Large font Middle font Small font]
On occasion you may need to examine a string of text, perhaps from a user submittal, to search out inappropriate material. This script provides the building blocks on how to examine a text string for a specific character or sequence of characters.

The script structure is contained in three parts.

1) The initial page setup which identifies the name of the script so it can resubmit to itself.
2) Page 1 which presents the data entry form to the user to get the string to be examined.
3) Page 2 which examines the string for a character or sequence of characters specified.

So, with that introduction, here's the script and how it's constructed.

Part 1.

Some standard code is introduced at the beginning to prevent cacheing of the page, and the name of the page is collected as well. Following that a set of client side scripts are included to examine the submitted string of text.


<%@ LANGUAGE="VBSCRIPT" %>

<%
    Response.Buffer=true
    Response.AddHeader "cache-control", "private"
    Response.AddHeader "pragma", "no-cache"
    Response.ExpiresAbsolute = #January 1, 1990 00:00:01#
    Response.Expires=-1


strScriptName = Request.ServerVariables("SCRIPT_NAME")

%>


The first function incorporates some DHTML to render the submitted text on the examination page.


<HTML>
<HEAD><TITLE>Examining a String</TITLE>

<SCRIPT language="vbscript"><!--

function getstrtext(strTEXT)
    mystring.innerHTML = strTEXT
end function


The second function checks for null or empty values to be used as the search criteria.


function findstring(strTEXT,cValue)
    if (IsNull(cValue) or cValue = "") then
        msgbox "Specify search value"
    else
        Call GetCount(strTEXT,cValue)
    end if
end function


The third function does the work of analyzing the content of the submitted string, searching for a match of the specified characters. It does this by using a split function splits the submitted string on each instance of the specified characters. Once found, the instance is counted.
The output of the findings again uses DHTML to render a summary of the count of the instances.


function GetCount(strTEXT,cValue)
    Dim I, A , J

    J=0

    A = split(strTEXT,cValue)
    For I = 1 to UBOUND (A)
    J=J+1
    next

    spanFound.innerHTML = J & " instances of <FONT color='red' >" &_
                 cValue & "</font> found!"

end function


The last subroutine is only for the convenience of this example. It allows the user to start a new test.


sub StartOver()
    location.href="<%=StrScriptName%>"
end sub
-->
</SCRIPT>
</HEAD>


Part 2.

This is the first page the user will see when the script runs. It is a form that collects the user's name and email address. It uses the 'get' method for passing the data.
The form also adds a flag to prevent the form from reappearing after the page resubmits to itself.


<BODY <%IF Request("PAGE")=2 then%>onload="getstrtext('<%=request("USERINPUT")%>')"<%end if%>>
<%'//Page 1 collect the input string to examine%>

<%If request("page")="" or request("page")=1 then%>
<H1>String Examination</H1>
<HR color="silver">
<form action=<%=StrScriptName%> method="post">
<INPUT TYPE=HIDDEN NAME="PAGE" VALUE=2>
<INPUT TYPE="Text" NAME="USERINPUT" size="100" Value="To count the instances of a character, or characters, in a text string, copy here and click SUBMIT, or use this string.">
<BR><BR>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</form>
<%end if%>


Part 3.

This is the second page designed to collect the specified characters to search for in the submitted text. The client side subroutine are executed and the results are presented.


<%'//Page 2 examine the input string

If request("page")=2 then%>

<DIV ID= mystring style="position:absolute; top:20; font-size:14pt;"></DIV>
<DIV STYLE="position:absolute; top:100"> 
<BUTTON style="background:blue; color:white; width:200; text-align:center"
    onclick="findstring '<%=request("USERINPUT")%>',txtTextToFind.value">Count instances of
</BUTTON>
<INPUT TYPE=TEXT ID=txtTextToFind VALUE="string">
<P><SPAN ID=spanFound></SPAN>

<HR color="silver"><BR>
<BUTTON ID="Button1" OnClick="StartOver()">Start Over</Button>
<%end if%>
</DIV>
</BODY>
</HTML>

(From: aspkey)

Relative article:
Relative software: