Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kristina 33 posts 49 karma points
    Nov 08, 2013 @ 15:30
    Kristina
    0

    Validation comment field

    Hello everybody, i need your help:)

    At my website I wrote some news with the possibility to comment them.. For every comment i create a new 'site' with document type "Kommentar".

    Now, because a lot of people are posting stupid URLs, i want to check if the comment contains an url.. If yes, it can't be posted.

    Here is my code:

    @{
      var successfulPost false;
      var name Request["name"];
      var message Request["message"];
    }
      
      if (IsPost){
        if (name!="" &message !=""){
                                    
          var dt DocumentType.GetByAlias("Kommentar");
     
          @Make sure its valid document type alias *@
       
          if (dt != null){
       
            var author umbraco.BusinessLogic.User.GetUser(0);
     
            var doc Document.MakeNew("Kommentar"dtauthorModel.Id);
            doc.getProperty("kommentarName").Value name;
            doc.getProperty("kommentarText").Value message;
     
         
            @Tell umbraco to publish the document *@
         
            doc.Publish(author);
            umbraco.library.UpdateDocumentCache(doc.Id);
         
            successfulPost true;
          }
        }
      }
    }

  • Mike 35 posts 100 karma points
    Nov 08, 2013 @ 15:39
    Mike
    0

    the line: if (name!="" && message !="")

    @* searching the message for "href" *@ becomes: if (name!="" && message !="" && culture.CompareInfo.IndexOf(message, "href", CompareOptions.IgnoreCase) == 0)

  • Kristina 33 posts 49 karma points
    Nov 08, 2013 @ 15:43
    Kristina
    0

    Thanks!

    But what does "culture" stand for?

  • Mike 35 posts 100 karma points
    Nov 08, 2013 @ 16:28
    Mike
    0

    Sorry.

    I just copied the code out of a multi-language site that I have.... where I define culture in my loop.

    Change to:

    System.Threading.Thread.CurrentThread.CurrentCulture.CompareInfo.IndexOf(message, "href", System.Globalization.CompareOptions.IgnoreCase);

  • Kristina 33 posts 49 karma points
    Nov 08, 2013 @ 17:16
    Kristina
    0

    Now no comment is posted.. Even whitout an url inside.. :S

    Maybe an other solution?

  • Mike 35 posts 100 karma points
    Nov 08, 2013 @ 17:20
    Mike
    0

    Can you post the 'if' statement as you have it now?

  • Kristina 33 posts 49 karma points
    Nov 08, 2013 @ 17:29
    Kristina
    0

    if (name!="" &message !="" &System.Threading.Thread.CurrentThread.CurrentCulture.CompareInfo.IndexOf
    (message"href"System.Globalization.CompareOptions.IgnoreCase)==0){}

  • Mike 35 posts 100 karma points
    Nov 08, 2013 @ 18:06
    Mike
    0

    It seems as if the CompareInfo is returning a non zero in your case... hmmm.

    OK how about you try this one:

    string msg = message ?? ""; string findMsg = "href"; msg = msg.ToLower();

    if ((name != "") && (message != "") && (!msg.Contains(findMsg)) { ... }

  • Kristina 33 posts 49 karma points
    Nov 09, 2013 @ 22:08
    Kristina
    0

    Doesn't work.. Now i can post everything.. :(

  • Mike 35 posts 100 karma points
    Nov 09, 2013 @ 23:02
    Mike
    0

    Now everything posts? Same results as your original post? 

  • Kristina 33 posts 49 karma points
    Nov 10, 2013 @ 13:27
    Kristina
    0

    Yes

     

  • Mike 35 posts 100 karma points
    Nov 11, 2013 @ 14:12
    Mike
    0

    Can you paste a sample value of your message? It's not finding href in message.

  • Kristina 33 posts 49 karma points
    Nov 11, 2013 @ 20:56
    Kristina
    0

    Oh no :( now it says ERROR (The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.) when i try this code: WHY?? :(

    @{
      var successfulPost false;
      var name Request["name"];
      var message Request["message"];


    @Handle form postcreate new document if valid post*@
      
    @{
      if (IsPost){
        string msg message ?""
        string findMsg "href"
        msg msg.ToLower();
        if((name != ""&(message != ""&(!msg.Contains(findMsg)){                                
          var dt DocumentType.GetByAlias("Kommentar");
     
          @Make sure its valid document type alias *@
       
          if (dt != null){
       
            var author umbraco.BusinessLogic.User.GetUser(0);
     
            var doc Document.MakeNew("Kommentar"dtauthorModel.Id);
            doc.getProperty("kommentarName").Value name;
            doc.getProperty("kommentarText").Value message;
     
         
            @Tell umbraco to publish the document *@
         
            doc.Publish(author);
            umbraco.library.UpdateDocumentCache(doc.Id);
         
            successfulPost true;
          }
        }
      }
    }

     

    WHY?? :(

     

  • Mike 35 posts 100 karma points
    Nov 11, 2013 @ 21:14
    Mike
    0

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @using umbraco.BusinessLogic;

    @using umbraco.cms.businesslogic.web;

     

    @{

      var successfulPost = false;

      var name = Request["name"];

      var message = Request["message"];

     

      if (IsPost){

        string msg = message ?? ""; 

        string findMsg = "href"; 

        msg = msg.ToLower();

        if((name != "") && (message != "") && (!msg.Contains(findMsg))) {                                

          var dt = DocumentType.GetByAlias("Kommentar");

     

          @* Make sure its a valid document type alias *@

       

          if (dt != null){

       

            var author = umbraco.BusinessLogic.User.GetUser(0);

     

            var doc = Document.MakeNew("Kommentar", dt, author, Model.Id);

            doc.getProperty("kommentarName").Value = name;

            doc.getProperty("kommentarText").Value = message;

     

         

            @* Tell umbraco to publish the document *@

         

            doc.Publish(author);

            umbraco.library.UpdateDocumentCache(doc.Id);

         

            successfulPost = true;

          }

        }

      }

    }

     

     

  • Kristina 33 posts 49 karma points
    Dec 01, 2013 @ 20:21
    Kristina
    0

    I tried your code and can still post an url :S

    www.honsfeldersv.be is the site.. if you select a news there is the possibility to comment it

  • Mike 35 posts 100 karma points
    Dec 02, 2013 @ 17:02
    Mike
    0

    Kristina,  The code is searching for "href", but if the commenter is not putting in a full html anchor tag, it won't find anything and will let it pass.  Maybe if you change the line:

     string findMsg = "href";

    to:

     string findMsg = "www";

    That might solve your problem.

  • Kristina 33 posts 49 karma points
    Dec 12, 2013 @ 14:43
    Kristina
    0

    Thank you, sorry i'm not used in programming in umbraco.

    And if i want to search for 'www' or for 'http' in a string, can I do this? 

    I tried

    string findMsg = "www", "http";

    but that doesn't work :D

Please Sign in or register to post replies

Write your reply to:

Draft