防止用户在 asp.net 模型验证中的表单输入期间输入空格?

发布于 2024-10-13 15:32:38 字数 307 浏览 9 评论 0原文

我在 asp.net MVC 网站中使用模型验证。 我想要一个功能来阻止用户在测试框中输入空格并提交表单。

还有其他可用的验证属性,但我找不到任何阻止用户在输入文本框中仅输入空格的验证属性。

我可以为此开发一个自定义属性,但还有另一种称为正则表达式验证器的方法,我认为我可以轻松地使用它来实现此功能。 例如:我们可以设置一个具有正则表达式的属性来验证电子邮件。如果用户输入错误的电子邮件,立即显示电子邮件格式错误的消息。

我想使用相同的,但我不知道如果用户仅输入空格则验证表单输入字段的正则表达式。

请帮我看看这种正则表达式? 谢谢,

I have bee using model validation in asp.net MVC website.
I want to have a functionality to prevent user from entering whitespace in testbox and submit the form.

There are other validation attributes available, but i could not find any validation attribute that prevents user from entering only whitespace in the input textbox.

I could develop a custom attribute for this, but there is another method called regular expression validator which i think i could use easily to achieve this functionality.
For example: We can set an attribute that has a regular expression for validating email. if User enters wrong email, immediately a message is shown that email format is wrong.

I want use the same, but i don't know the regular expresison that validates a form input field if user enters only whitespace.

Please help me with this kind of regular expression?
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙气飘飘 2024-10-20 15:32:38
[RegularExpression(@"[^\s]+")]
public string Data { get; set; }
[RegularExpression(@"[^\s]+")]
public string Data { get; set; }
网白 2024-10-20 15:32:38

使用正则表达式验证与此模式:

^\S+$

这将只允许非空白。

(更新)

如果您希望用户输入空格但前提是其中有非空格:

\S+

Use Regex validation with this pattern:

^\S+$

This will allow only non-white-space.

(Update)

If you want users to enter whitespace but only if there are non-whitespace in there:

\S+
一个人的旅程 2024-10-20 15:32:38

这个正则表达式可能有效

^[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+(\\s+[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+)*$

This regular expression might work

^[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+(\\s+[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+)*$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文