【问题标题】:Validate TextBox for specific string验证特定字符串的文本框
【发布时间】:2014-06-14 10:20:02
【问题描述】:

我有一个文本框,最终将成为电子邮件的正文。我遇到的问题是有些人在此文本框中放置链接,当电子邮件发出时,正文中有一堆超链接。我想通过验证文本框来防止这种情况发生,因此当它在文本框中看到包含“http://”的文本时,会提示用户删除文本框中的所有链接,然后才能继续。

我需要设置 IsBodyHtml = true 因为我有一个图像也会自动插入到正文中。所以目前禁用对我来说不是一个选项。

<strong>Alert Description</strong><br>
<asp:TextBox ID="AlertDesTxtBox" Rows="15" Width="450" TextMode="MultiLine"
             runat="server" />
<asp:RequiredFieldValidator id="RFV3" runat="server" ControlToValidate="AlertDesTxtBox"
             ErrorMessage="Description is required."
             ForeColor="Red">
</asp:RequiredFieldValidator>

有什么建议吗?自定义验证器?

【问题讨论】:

  • 你看过使用正则表达式了吗?
  • 同意@AMR,这似乎是一个正则表达式被发明来解决的问题。
  • @AMR 我现在会检查一下,感谢您指出我不知道正则表达式的正确方向!
  • @kcray 这就是社区在这里的目的!如果你需要一个好的资源,我很确定Pluralsight.com 有一个非常好的关于如何使用正则表达式的视频列表。它们非常强大。

标签: c# .net validation textbox


【解决方案1】:

您可以轻松搜索“http://”:

int startIndex = 0, remaining = textBox.Text.Length;

while ((startIndex = textBox1.Text.IndexOf(startIndex, textBox.Text.Length - startIndex)) > 0)
{
    MessageBox.Show("There is a link in here");
    startIndex++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 2012-12-21
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多