Filter TextBox content to discourage use of certain keywords

Last post 03-26-2006 10:40 AM by NC01. 3 replies.

Sort Posts:

  • Filter TextBox content to discourage use of certain keywords

    03-25-2006, 8:01 AM
    My problem is the following: I have a web application where people have a super-high tendency to put irrelevant keywords in their message title (typically example "Please Help" as a message title to the technical support team).

    I would like to to strongly discourage the use of a list of "blacklisted" keywords as inputs for a given TextBox WebControl. I am currently think of a small script that would appear to display a warning message when a blacklisted keyword is entered.

    Does anyone knows how to achieve such feature on client-side?

    Thanks in advance,
    Joannès
  • Re: Filter TextBox content to discourage use of certain keywords

    03-26-2006, 8:53 AM
    • All-Star
      74,987 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,946
    • TrustedFriends-MVPs

    In the CodeBehind, Page_Load event handler, copy the key word list client-side:
    string keyWordList = string.Empty;
    for (int i=0; i<m_KeyWordList.Length; i++)
    {
     if ( keyWordList.Length > 0 )
      keyWordList += ",";

     keyWordList += "new String('" + m_KeyWordList[i] + "')";
    }

    // keyWordList = "new String('word1'),new String('word2'), ...etc";
    RegisterArrayDeclaration("JsKeyWordArrayArray", keyWordList);

    // Add the validation script:
    string validateTextBoxScript =
         "<script language=JavaScript>\n" +
         "function validateTextBox(elementRef)\n" +
         "{\n" +
         " var textBoxValue = elementRef.value;\n" +
         "\n" +
         " if ( textBoxValue == '' )\n" +
         "  return;\n" +
         "\n" +
         " for (var i=0; i<JsKeyWordArrayArray.length; i++)\n" +
         " {\n" +
         "  if ( textBoxValue == JsKeyWordArrayArray[i] )\n" +
         "   alert('WARNING: The word ' + JsKeyWordArrayArray[i] + ' is black-listed.');\n" +
         " }\n" +
        "}\n" +
         "</script>";

    RegisterClientScriptBlock("ValidateTextBoxScriptScript", validateTextBoxScript);

    // Add the client-side event handler to the TextBox:
    TextBox1.Attributes.Add("onchange", "JavaScript: validateTextBox(this);");

    NC...

     

  • Re: Filter TextBox content to discourage use of certain keywords

    03-26-2006, 9:59 AM
    Ok, thanks for the ideas.

    Joannes
  • Re: Filter TextBox content to discourage use of certain keywords

    03-26-2006, 10:40 AM
    • All-Star
      74,987 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,946
    • TrustedFriends-MVPs

    In thinking about your problem, you probably should change this:
         "  if ( textBoxValue == JsKeyWordArrayArray[i] )\n" +
    to
         "  if ( textBoxValue.indexOf(JsKeyWordArrayArray[i]) != -1 )\n" +

    since your TextBox is going to have multiple words.

    NC...

Page 1 of 1 (4 items)