什么是约束验证不允许在ASP.NET任何空间的最佳方式 [英] What's the best way to constraint validation to not allow any spaces in ASP.NET

查看:201
本文介绍了什么是约束验证不允许在ASP.NET任何空间的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的情况:


  1. 不允许有空格在所有

  2. 不允许有开头空格或​​字符串末尾只

..一个小问题,它是很好的通过RegularEx pressionValidator的正则表达式检查(验证)的输入空间?

..A little question, is it good to check (to validate) the input for spaces through the RegEx of the RegularExpressionValidator ?

推荐答案

在您的previous问题,你提到你从0到50个字符通缉。如果仍然是这样,这里有你想要什么:

In your previous question, you mentioned you wanted from 0 to 50 characters. If that's still the case, here's what you want:

/^\S{0,50}$/
/^(?!\s).{0,50}(?<!\s)$/

截至目前,我觉得这些都是贴在正则表达式只允许与第一图案不到一个字母,并与第二图案不到两个字母。

As of right now, I think these are the only regexes posted that allow for less than one letter with the first pattern, and less than two letters with the second pattern.

的正则表达式不是坏一点,他们只是不适合每个任务的专用工具。如果你想验证输入在ASP.NET中,我肯定会用一个RegularEx pressionValidator这个特定的模式,否则你将不得不浪费你的时间写的CustomValidator为pretty微薄的性能提升。 <一href=\"http://stackoverflow.com/questions/5668947/python-string-replace-vs-re-sub/5670379#5670379\">See我回答这个问题,其他,获取有关何时以及何时不使用正则表达式一些指导。

Regexes are not a "bad" thing, they're just a specialized tool that isn't suited for every task. If you're trying to validate input in ASP.NET, I would definitely use a RegularExpressionValidator for this particular pattern, because otherwise you'll have to waste your time writing a CustomValidator for a pretty meager performance boost. See my answer to this other question for a little guidance on when and when not to use regex.

在这种情况下,我会使用正则表达式验证器的原因有少做与模式本身并更多地与ASP.NET。一个RegularEx pressionValidator只需拖放到你的ASPX code,以及所有你必须​​写是正则表达式的10-21个字符。用的CustomValidator,你必须编写自定义的验证功能,无论是在codebehind和JavaScript。你可能会挤压出它的更高的性能,但想想当验证进场:只有一次每个回发的性能差异将是小于一毫秒。它根本不值得你花时间作为一个开发者 - 您或您的雇主。记住:硬件是便宜,程序员很贵 premature优化是一切罪恶的根源。

In this case, the reason I'd use a regex validator has less to do with the pattern itself and more to do with ASP.NET. A RegularExpressionValidator can just be dragged and dropped into your ASPX code, and all you'd have to write would be 10-21 characters of regex. With a CustomValidator, you'd have to write custom validation functions, both in the codebehind and the JavaScript. You might squeeze a little more performance out of it, but think about when validation comes into play: only once per postback. The performance difference is going to be less than a millisecond. It's simply not worth your time as a developer -- to you or your employer. Remember: Hardware is cheap, programmers are expensive, and premature optimization is the root of all evil.

这篇关于什么是约束验证不允许在ASP.NET任何空间的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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