RegularEx pressionValidator不开火白色空间进入 [英] RegularExpressionValidator not firing on white-space entry

查看:159
本文介绍了RegularEx pressionValidator不开火白色空间进入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证只包含数字串。容易验证?我说RegularEx pressionValidator,与ValidationEx pression =/ D +。

I want to validate string containing only numbers. Easy validation? I added RegularExpressionValidator, with ValidationExpression="/d+".

看起来不错 - 但只输入空间时,没有验证!甚至很多空间被验证还好。我不需要这是强制性的。

Looks okay - but nothing validated when only space is entered! Even many spaces are validated okay. I don't need this to be mandatory.

我可以修剪服务器上,但不能经常EX pression做的一切!

I can trim on server, but cannot regular expression do everything!

推荐答案

这是由设计,而且往往引发许多人赶走。该RegularEx pressionValidator不会使现场强制性,并允许它是空白的,并接受空格。该\\ D +格式是正确的。即使使用^ \\ D + $将导致允许空白的同样的问题。迫使这禁止空白,唯一的方法是还包括一个RequiredFieldValidator在同一控制操作。

This is by design and tends to throw many people off. The RegularExpressionValidator does not make a field mandatory and allows it to be blank and accepts whitespaces. The \d+ format is correct. Even using ^\d+$ will result in the same problem of allowing whitespace. The only way to force this to disallow whitespace is to also include a RequiredFieldValidator to operate on the same control.

这是每<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularex$p$pssionvalidator.aspx\">RegularEx$p$pssionValidator文档,其中规定:

验证成功,如果输入
  控制是空的。如果值
  所需的相关输入
  控制,使用RequiredFieldValidator
  控制除
  RegularEx pressionValidator控制。

Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.

如预期定期EX pression领域中的code-后面检查将工作;这仅仅是与RegularEx pressionValidator的问题。所以你可以想见,使用的CustomValidator,而不是说 args.IsValid = Regex.IsMatch(txtInput.Text,@^ \\ D + $),如果它包含空格那么它将返回false。但是,如果是这样的话,为什么不直接使用按照文档的RequiredFieldValidator,避免编写自定义code?又一个的CustomValidator意味着强制回发(除非您指定具有同等的JavaScript正则表达式客户端验证脚本)。

A regular expression check of the field in the code-behind would work as expected; this is only an issue with the RegularExpressionValidator. So you could conceivably use a CustomValidator instead and say args.IsValid = Regex.IsMatch(txtInput.Text, @"^\d+$") and if it contained whitespace then it would return false. But if that's the case why not just use the RequiredFieldValidator per the documentation and avoid writing custom code? Also a CustomValidator means a mandatory postback (unless you specify a client validation script with equivalent javascript regex).

这篇关于RegularEx pressionValidator不开火白色空间进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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