locked
CustomValidator Stop SPAM RRS feed

  • Question

  • User-1653812950 posted

    I would like to use the <ASP:CustomValidator> control to block Spam. Suggestions on how to do this?

    I can find Regex expressions at: www.regexlib.com but don't know if they would apply to a CustomValidator.

    Thank you in advance for any help.

    Thursday, February 7, 2019 11:28 PM

Answers

  • User-1653812950 posted

    Before I mark "Answer" for any of these, I am going to take some time to look through different options. However, since I was getting Spam through my "Contact Me" page and "Error Page" which sends me an email notification of what the person entered, I did figure out a good solution. I wrote a routine that searches the "Message" box and will not allow an email to be sent to me if it finds specific words in the text. So far this has worked.

    Here is the VB file code:


    Namespace nsMail

      Public Class MailOptions

        Public SpamDetect01 As String ' Used in Main ASPX page

        Public Function SpamCheck(ByVal TextBoxLen As Integer, ByVal TextBoxText As String)

          Dim StrSearch As String
          Dim Spin1 As Integer
          Dim ListItemLen As Integer
          Dim ListItem As String
          SpamDetect01 = "No"

          Dim SpamWords() As String = {"$", "pen", "viagra", "offer", "+", "profit", "omg", _
          "per week", "per day", "per month", "invest", "€", "par mois", "girl", ".ru", "win"} ' More or less words can be added.

          For SpinWords As Integer = 0 To 15 ' THIS CHANGES WITH THE AMOUNT OF ITEMS IN "SpamWords"

            ListItem = SpamWords.GetValue(SpinWords) ' Each word in the list
             ListItemLen = ListItem.Length ' Length of each word in the list.

              For Spin1 = 1 To TextBoxLen

                StrSearch = Mid(TextBoxText, Spin1, ListItemLen)

               If StrSearch = ListItem Then
                SpamDetect01 = "Yes"
                 ' Response.Write(StrSearch & "<br />")
                 End If

              Next ' Spin1

          Next ' SpinWords

       End Function

      End Class

    End Namespace

    I did not write code for the entire page. I am only listing the necessary parts for this routine to work. It is expected that anyone who reads this knows how to program the rest of an ASPX PAGE. Here is the Main ASPX page code:

    <%@ Import Namespace="nsMail" %> ' Imports the nsMail function from the VB file

    ' This is the button routine the User Clicks to send me an Email after all necessary information is filled out:

    Sub SendMeEmail_Click(ByVal Source As Object, ByVal E As EventArgs)

        If tbMsgFromUser.Text <> "" Then

        Dim azLoadEmail As New MailOptions

        ' tbMsgFromUser is the text from a TEXTBOX containing the User's message to me.
        ' All text from the TEXTBOX is made into lower case, so I do not have to write
        ' a variety of options in the SPAM Word List in the VB file.
        azLoadEmail.SpamCheck(tbMsgFromUser.Text.Length, tbMsgFromUser.Text.ToString.ToLower)

        Dim ContainSpam As String = azLoadEmail.SpamDetect01

        ' Here is where I am writing information based on the User's input
        ' Today send an email to myself with the content the User filled
        ' out on this page via several TEXTBOXES.

        If ContainSpam = "No" Then ' There is no Spam in main message

        lblWarning.Visible = False ' The Spam warning LABEL is turned off since
        ' there is no Spam in the Main Message from the User.

        ' Here I place some information needed to send me an email if there is No Spam
        ' in the main Message from the User.

        ' A message is placed here for the User letting them know I got the message
        ' and they will be notified.

        Else ' There is Spam in the message

        lblWarning.Visible = True ' Spam warning LABEL becomes visible if SPAM is detected in message.
        ' Email does not get sent to me because of Spam content.

        ' I do, however, send myself a message letting me know someone tried
        ' to send me SPAM, and it also sends me their IP address so I can block them
        ' from coming to my site in the future.

        End If

        ' Routine gets placed here to send email to me if all previous qualifications
        ' are met and there is no Spam in the main content. User has to fill in specific information using Validators to even
        ' get to this point.

        End If

    End Sub

    This has successful worked to block Spam emails from coming to me via my 2 web pages.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2019 6:06 PM

All replies

  • User-1174608757 posted

    Hi qwsoftdraw,

    According to your description,I don't think you could use CustomValidator to prevent SPAM. In fact ,without a captcha there is no 100% way of stopping all spam. (or even with a captcha).Spam is a problem that all website owners struggle to deal with.What we could do  is trying to reduce it.

    Here are some links about how to reduce spam. I hope it could help you.

    https://www.lifewire.com/solutions-to-protect-web-forms-from-spam-3467469

    https://stackoverflow.com/questions/16786229/how-to-prevent-from-spammers-while-submitting-web-form-in-asp-net

    https://social.msdn.microsoft.com/Forums/en-US/451175d1-0b2a-4189-a8e4-6e812af1bd0c/cnet-code-to-stop-spam-mailsjunk-mails?forum=csharplanguage

    https://mtstudios.net/the-honey-trap-the-easiest-way-ive-found-to-stop-spam-emails-on-asp-net-contact-forms/

    Best Regards

    Wei Zhang

    Friday, February 8, 2019 5:15 AM
  • User-1653812950 posted

    What I am specifically looking for is a way to search a body of text for specific words using the CustomValidator. Thanks.

    Friday, February 8, 2019 5:29 PM
  • User409696431 posted

    The documentation for CustomValidator gives examples of both server-side and client side validation.  You can certainly write a validation routine that searches the text for a list of words.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.customvalidator?view=netframework-4.7.2

    Saturday, February 9, 2019 12:29 AM
  • User-1653812950 posted

    Before I mark "Answer" for any of these, I am going to take some time to look through different options. However, since I was getting Spam through my "Contact Me" page and "Error Page" which sends me an email notification of what the person entered, I did figure out a good solution. I wrote a routine that searches the "Message" box and will not allow an email to be sent to me if it finds specific words in the text. So far this has worked.

    Here is the VB file code:


    Namespace nsMail

      Public Class MailOptions

        Public SpamDetect01 As String ' Used in Main ASPX page

        Public Function SpamCheck(ByVal TextBoxLen As Integer, ByVal TextBoxText As String)

          Dim StrSearch As String
          Dim Spin1 As Integer
          Dim ListItemLen As Integer
          Dim ListItem As String
          SpamDetect01 = "No"

          Dim SpamWords() As String = {"$", "pen", "viagra", "offer", "+", "profit", "omg", _
          "per week", "per day", "per month", "invest", "€", "par mois", "girl", ".ru", "win"} ' More or less words can be added.

          For SpinWords As Integer = 0 To 15 ' THIS CHANGES WITH THE AMOUNT OF ITEMS IN "SpamWords"

            ListItem = SpamWords.GetValue(SpinWords) ' Each word in the list
             ListItemLen = ListItem.Length ' Length of each word in the list.

              For Spin1 = 1 To TextBoxLen

                StrSearch = Mid(TextBoxText, Spin1, ListItemLen)

               If StrSearch = ListItem Then
                SpamDetect01 = "Yes"
                 ' Response.Write(StrSearch & "<br />")
                 End If

              Next ' Spin1

          Next ' SpinWords

       End Function

      End Class

    End Namespace

    I did not write code for the entire page. I am only listing the necessary parts for this routine to work. It is expected that anyone who reads this knows how to program the rest of an ASPX PAGE. Here is the Main ASPX page code:

    <%@ Import Namespace="nsMail" %> ' Imports the nsMail function from the VB file

    ' This is the button routine the User Clicks to send me an Email after all necessary information is filled out:

    Sub SendMeEmail_Click(ByVal Source As Object, ByVal E As EventArgs)

        If tbMsgFromUser.Text <> "" Then

        Dim azLoadEmail As New MailOptions

        ' tbMsgFromUser is the text from a TEXTBOX containing the User's message to me.
        ' All text from the TEXTBOX is made into lower case, so I do not have to write
        ' a variety of options in the SPAM Word List in the VB file.
        azLoadEmail.SpamCheck(tbMsgFromUser.Text.Length, tbMsgFromUser.Text.ToString.ToLower)

        Dim ContainSpam As String = azLoadEmail.SpamDetect01

        ' Here is where I am writing information based on the User's input
        ' Today send an email to myself with the content the User filled
        ' out on this page via several TEXTBOXES.

        If ContainSpam = "No" Then ' There is no Spam in main message

        lblWarning.Visible = False ' The Spam warning LABEL is turned off since
        ' there is no Spam in the Main Message from the User.

        ' Here I place some information needed to send me an email if there is No Spam
        ' in the main Message from the User.

        ' A message is placed here for the User letting them know I got the message
        ' and they will be notified.

        Else ' There is Spam in the message

        lblWarning.Visible = True ' Spam warning LABEL becomes visible if SPAM is detected in message.
        ' Email does not get sent to me because of Spam content.

        ' I do, however, send myself a message letting me know someone tried
        ' to send me SPAM, and it also sends me their IP address so I can block them
        ' from coming to my site in the future.

        End If

        ' Routine gets placed here to send email to me if all previous qualifications
        ' are met and there is no Spam in the main content. User has to fill in specific information using Validators to even
        ' get to this point.

        End If

    End Sub

    This has successful worked to block Spam emails from coming to me via my 2 web pages.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 11, 2019 6:06 PM