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 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; } } } }
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 post: create a new document if a 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 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, 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:
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 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;
}
}
}
}
the line: if (name!="" && message !="")
@* searching the message for "href" *@ becomes: if (name!="" && message !="" && culture.CompareInfo.IndexOf(message, "href", CompareOptions.IgnoreCase) == 0)
Thanks!
But what does "culture" stand for?
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);
Now no comment is posted.. Even whitout an url inside.. :S
Maybe an other solution?
Can you post the 'if' statement as you have it now?
if (name!="" && message !="" && System.Threading.Thread.CurrentThread.CurrentCulture.CompareInfo.IndexOf
(message, "href", System.Globalization.CompareOptions.IgnoreCase)==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)) { ... }
Doesn't work.. Now i can post everything.. :(
Now everything posts? Same results as your original post?
Yes
Can you paste a sample value of your message? It's not finding href in message.
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 post: create a new document if a 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 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;
}
}
}
}
WHY?? :(
@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;
}
}
}
}
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
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.
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
is working on a reply...