2012-11-30 95 views
1

我有一個可接受HTML標記的多行文本框。 但是鏈接不允許作爲用戶可以輸入的HTML標籤的一部分。 換句話說,在此文本框中不能包含任何href標記作爲值。 我需要爲此進行驗證。 我該如何去做這件事? 我是新來的.net,所以我不知道如何實現這一點。 請幫忙。如何找出我的多行文本框中可接受HTML標記的值是否具有href標記,即<a href=></a>標記

更新了此內容。我創建了一個asp正則表達式驗證器。但它給我的語法錯誤。 我在做什麼錯?我是.net和Regex的新手。請幫忙。

<asp:RegularExpressionValidator id="EmailLinkValidator" runat="server" controltovalidate="EmailTextBox" display="Dynamic" ValidationExpression="(?m:(?:<a)?href|</a>\r?\n?)" cssclass="clsError" errormessage="<%=this.ERR_MSG%>">*</asp:RegularExpressionValidator> 
+0

這是Asp.Net – Ditty

+0

參見[消毒] (http://code.google.com/p/stack-exchange-data-explorer/source/browse/App/StackExchange.DataExplorer/Helpers/HtmlUtilities.cs)方法。 –

+0

所以我只需要爲_whitelist_a實現Sanitize方法,如果我只想刪除 Ditty

回答

0

,您可以使用正則表達式來測試,如果文本包含「A」標籤

string text = "<div class='answer-help'><p> <a href='http://stackoverflow.com/questions/how-to-answer'>tips</a></p></div>"; 
    System.Text.RegularExpressions.Regex ex = new System.Text.RegularExpressions.Regex("<a .*>.*</a>"); 
    bool containsATag = ex.IsMatch(text); 

然後,它會返回一個真正的

+0

Would your regex catch this? '' :) –

+0

I am new to .net. But supposedly Yes as I am sanitizing the HTML tags before saving. So no scripts are allowed. But I should not be saving any HREF tags specifically and should give an alert to the user in this case. Hence wanted to check that specifically. – Ditty

+0

Please help.. I am still stuck. – Ditty

相關問題