1

我有一个文本框:

<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server" 
    ValidationExpression="^[a-zA-Z0-9@+'.!#$',:;=/\(\),\-\s]{1,255}$"
    ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
    ErrorMessage="You have entered a character(s) that is not allowed in the title." />

我也想允许 " 字符。如何修改正则表达式字符串???

我试过这个:

<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server" 
    ValidationExpression="^[a-zA-Z0-9@+'.!#$'\",:;=/(),\-\s]{1,255}$"
    ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
    ErrorMessage="You have entered a character(s) that is not allowed in the title." />

<asp:RegularExpressionValidator ID="ValidateTitleCharacters" runat="server" 
    Validat‌​ionExpression="^[a-zA-Z0-9@+'.!#$',:;=/()(""),\-\s]{1,255}$"
    ControlToValidate="title" Text="You have entered a character(s) that is not allowed in the title."
    ErrorMessage="You have entered a character(s) that is not allowed in the title." />

两次尝试都在破坏字符串。

4

1 回答 1

13

从您发布的片段来看,正则表达式似乎嵌入在标记中 - 这意味着您需要将双引号字符转义为 HTML 字符实体。

使用&quot;

ValidationExpression="^[a-zA-Z0-9@+'.!#$'&quot;,:;=/\(\),\-\s]{1,255}$"

ASP.NET 引擎会将字符实体转换为".

或者,ValidationExpression在后面的代码中设置值(OnInit例如,在 中):

ValidateTitleCharacters.ValidationExpression = 
                                  "^[a-zA-Z0-9@+'.!#$'\",:;=/\(\),\-\s]{1,255}$";
于 2012-11-26T13:08:39.913 回答