1

I've got a simple spam killer I'm trying to put together, but the text is not showing up on my form.

The javascript is:

<script language="javascript" type="text/javascript">
    document.write("SPAM Killer: What is " + GetDateMonth() + " + " + GetDateDay() + "?")
</script>

In my .js file, I have these two functions defined:

function GetDateMonth() {
  return date1.getMonth() + 1;
}

function GetDateDay() {
  return date1.getDay() + 1;
}

The text shows up under IE8, but not under Chrome.

As a bonus: My OnClick method of my Submit form has this bit of code that is incorrectly adding my month and date:

  string spamError = "The SPAM Killer answer was incorrect. ";
  char[] split = spamTest.ToCharArray();
  for (int i = 0; i < split.Length; i++) {
    if (char.IsLetter(split[i])) {
      Ok = false;
      txtMessage.Text = spamError + "Non-numeric data entered.";
      return;
    }
  }
  int nTestValue = Convert.ToInt32(spamTest, 10);
  if (nTestValue < 1) {
    Ok = false;
    txtMessage.Text = spamError + "Negatave or zero value found.";
  }
  DateTime dt = DateTime.Now;
  int month = dt.Month;
  int day = dt.Day;
  int nCorrect = month + day;
  if (nCorrect != nTestValue) {
    Ok = false;
    txtMessage.Text = spamError + string.Format("Expected {0}; Received {1}.", nCorrect, nTestValue);
    return;
  }

Using IE8, I see the following:

SPAM Killer: What is 2 + 3?

I enter 5, click Send, and get Expected 17; Received 5.

2 Answers2

1

Don't reinvent the wheel, help read books with http://www.google.com/recaptcha

For C# code see http://code.google.com/apis/recaptcha/docs/aspnet.html

If you're adamant on sticking with your code, think about the problems around midnight, and users in other timezones. Also, a bot can very easily answer your anti-bot question, it would take me 45 seconds to code support for that, if I wrote bots.

If you're still adamant, you shouldn't use document.write anymore (not since 2002), but instead use DOM to insert the text to a tag ID like this: Change label text using Javascript

Community
  • 1
  • 1
servermanfail
  • 2,532
  • 20
  • 21
  • This is a HOSTED website. I do not have the option of installing 3rd party apps. I did give it a go, but I ran into a Security Exception: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. –  Feb 15 '11 at 22:26
  • Also, I called our Tech Support, and this option is not available for dynamic IP addresses. –  Feb 15 '11 at 22:40
  • Gawd people! I'm not worried about the logic behind my code. It's just a personal website, for crying out loud. The problem is no text shows up in Chrome. Can someone kindly address the question? –  Feb 16 '11 at 18:58
0

The answer, it seems, was in using the document.write() function with appending strings.

I redesigned my HTML to be more like this:

<table>
  <tr>
    <td colspan="2">
      <b>[Human Check]</b><br />
      Enter the text to the left and below exactly as it appears:
    </td>
  </tr>
  <tr>
    <td>
      <script language="javascript" type="text/javascript">
        document.write(GetSpamText())
      </script>
    </td>
  </tr>
</table>

@serverfault: Thanks for your suggestion about the date property, though. That would have been a problem.

The text returned by GetSpamText() can be static or coded to create a random value (another topic).