输入/返回键触发RegularExpressionValidator

时间:2017-04-21 14:26:42

标签: asp.net regex

我有一个asp:Textbox,我试图确保用户不会复制并粘贴到Tags中。以下代码适用于检查<和>但如果用户点击Enter或Return键在文本框中创建新行,它也会抛出错误消息。我需要对ValidationExpression进行哪些更改以允许用户能够使用Enter / Return键,但不允许<或者>钥匙?

 <asp:RegularExpressionValidator ID="RegularExpressionVal3" runat="server"
                                    ErrorMessage="You cannot put tags into the text box (e.g. &lthtml&gt)"
                                    ControlToValidate="txtMSPC"
                                    ValidationExpression="^(?!<.*?>).*" ForeColor="Red" />

1 个答案:

答案 0 :(得分:0)

仅限客户端验证,您可以使用

ValidationExpression="^(?![\\s\\S]*<[^>]*>)[\\s\\S]*"

详细

  • ^ - 字符串开头
  • (?![\\s\\S]*<[^>]*>) - 没有<后面跟着>以外的0 +字符,然后是字符串中的>
  • [\\s\\S]** - 任何0+字符
相关问题