RegularEx pressionValidator限制输入长度与字符类型没有限制 [英] RegularExpressionValidator to limit input length with no limitations on character types

查看:107
本文介绍了RegularEx pressionValidator限制输入长度与字符类型没有限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 RegularEx pressionValidator 来验证文本框输入长度。我有工作,但只接受字母和数字。我想,让任何人物,唯一的检查是有不超过25个字符。

 < ASP:文本框ID =MenuLabel=服务器/>
< ASP:RegularEx pressionValidator ValidationEx pression =^ [A-ZA-Z0-9] {25} $ID =MenuLabelVal=服务器的ErrorMessage =菜单标签必须不再超过25个字符。的ControlToValidate =MenuLabel/>
 

正前pressions是不是我的强项,所以有人可以告诉我,我该怎么修改 ^ [A-ZA-Z0-9] {25} $ 重新present的的任何字符的高达25倍,而不是仅仅字母和数字。

注意1:我已经有一个的RequiredFieldValidator ,以确保1个或多个字符

注意2:我知道我可以只使用了最大长度属性的文本框,但是这种形式也是编辑现有的数据,并我不希望它在编辑时简单地截断现有的记录。我想preFER实现一个验证器,这使得它很明显用户编辑现有的数据,他们需要缩短的价值,而不是形式截断无需用户实现了。

注意3:我接受替代方案,如自定义验证,只要它仅依赖于客户端验证。我没有访问code-背后写在服务器上的一个自定义的验证处理程序。

解决方案

  {1,25}
 

的值应限制到任何字符1至25倍。不知道这将如何应对UNI code输入,但 - 它可以指望多字节字符作为单个字符,因此,如果您要使用此限制的数据库要小心

可能你可能想 ^ {1,25} $ ,但我认为,即使这个简单的版本应该工作

I'm trying to use a RegularExpressionValidator to validate a TextBox input length. I have it working, but it only accepts letters and numbers. I'd like to allow any characters, with the only check being there are no more than 25 characters.

<asp:TextBox ID="MenuLabel" runat="server" />
<asp:RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9]{25}$" ID="MenuLabelVal" runat="server" ErrorMessage="Menu Label must be no longer than 25 characters." ControlToValidate="MenuLabel"  />

Regular expressions are not my strong suit, so can someone tell me how I modify ^[a-zA-Z0-9]{25}$ to represent any characters up to 25 times rather than just alpha-numeric.

Note 1: I already have a RequiredFieldValidator in place to ensure 1 or more characters.

Note 2: I know I could just use the MaxLength property on the TextBox, however this form is also for editing existing data, and I do not want it to simply truncate existing records when editing. I'd prefer to implement a validator which makes it obvious for users editing existing data they need to shorten the value, rather than the form truncating without the user realizing.

Note 3: I'm open to alternate solutions, such as a custom validator, as long as it relies only on client-side validation. I don't have access to the code-behind to write a custom validate handler on the server.

解决方案

.{1,25} 

should limit the value to any char 1 to 25 times. Not sure how this will deal with unicode input though - it may count multi-byte chars as a single char, so be careful if you are using this to limit for a database

possibly you might want ^.{1,25}$ but I think even this simple version should work

这篇关于RegularEx pressionValidator限制输入长度与字符类型没有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆