简体   繁体   English

多行ASP.NET文本框的RegEx

[英]RegEx For Multiline ASP.NET Textbox

I want to prevent the entry of two characters together but I want the user to be able to enter one or the other as well as use the enter key. 我想防止两个字符一起输入,但是我希望用户既可以输入一个字符又可以使用回车键。 I would like to use a white list instead of black listing characters. 我想使用白名单而不是黑名单字符。 The regular expression also needs to support a min and max length. 正则表达式还需要支持最小和最大长度。 I'm doing client side validation using the asp:regularexpression control. 我正在使用asp:regularexpression控件进行客户端验证。 I do not want to have to do server side validation unless that's the only solution to this problem. 我不想做服务器端验证,除非这是解决此问题的唯一方法。 Has anyone else run into this? 还有其他人遇到吗? Thanks in advance for any help. 在此先感谢您的帮助。

Here is the regex I'm currently using: 这是我当前正在使用的正则表达式:

(?!. (&#))^[a-zA-Z0-9!@#$%^& _=+~''"";:, \\r\\n.()\\?-]{1,1000}$ (?!。 (&#))^ [a-zA-Z0-9!@#$%^& _ = +〜''“”;:,\\ r \\ n。()\\?-] {1, 1000} $

I'm using a asp.net textbox that is set to multiline so I have to allow for \\r and \\n or what's the point of using a multiline textbox :) 我正在使用一个设置为多行的asp.net文本框,因此我必须考虑\\ r和\\ n或使用多行文本框的意义是什么:)

I want to keep the user from entering &# together but allow them to enter text with & or # in it and allow all of the characters a-zA-Z0-9!@#$%^&*_=+~''"";:, \\r\\n.()\\?-. 我想让用户不要一起输入&#,但允许他们输入带有&或#的文本,并允许所有字符a-zA-Z0-9!@#$%^&* _ = +〜'' “”;:,\\ r \\ n。()\\?-。

A valid text entry would be as follows: 有效的文本输入如下:

I have a question about my order. 我对我的订单有疑问。 The order number is 12345. Can you help me? 订单号码为12345。您能帮我吗?

An invalid text entry would be as follows: 无效的文本输入如下:

I am trying to keep the user from entering &# in the textbox, but I want to allow them to enter & or #. 我试图阻止用户在文本框中输入&#,但是我想允许他们输入&或#。

string pattern = @"^(?!.*&#).{1,1000}$"

This asserts that the string does not contain the combination &# and otherwise allows anything between 1 and 1000 characters long, inclusive. 这断言该字符串不包含&#组合,否则允许长度在1到1000个字符(含)之间的任何字符。 If you really want the whitelist, replace the . 如果您确实需要白名单,请替换. preceding the { with your whitelist character class (inside [] ). {前面加上您的白名单字符类(在[]内部)。

这应该为您工作:

string pattern = @"^(?![\s\S]*&#)[\s\S]{1,1000}$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM