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
[RegularExpression(@"[^\s]+")]
public string Data { get; set; }
Use Regex validation with this pattern:
^\S+$
This will allow only non-white-space.
If you want users to enter whitespace but only if there are non-whitespace in there:
\S+
This regular expression might work
^[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+(\\s+[a-zA-Z0-9,-.@~!#$%&*<>?:;_='/()]+)*$