Prevent user from entering white space during form input in asp.net model validation?

后端 未结 3 503
执念已碎
执念已碎 2021-01-13 10:43

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.

The

相关标签:
3条回答
  • 2021-01-13 10:57
    [RegularExpression(@"[^\s]+")]
    public string Data { get; set; }
    
    0 讨论(0)
  • 2021-01-13 11:15

    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+
    
    0 讨论(0)
  • 2021-01-13 11:17

    This regular expression might work

    ^[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+(\\s+[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+)*$
    
    0 讨论(0)
提交回复
热议问题