且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是约束验证不允许在ASP.NET任何空间的***方式

更新时间:2023-02-16 12:30:18

在您的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微薄的性能提升。 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.