Prevent user from entering blank space while entering form into asp.net model validation?

I have use model validation on the MVC asp.net website. I want to have functionality so that the user cannot enter spaces in the test field and submit the form.

Other validation attributes are available, but I could not find a validation attribute that prevents the user from entering only spaces in the input text field.

I could develop a custom attribute for this, but there is another method called the regex validator, which I think could easily be used to achieve this functionality. For example: we can set an attribute that has a regular expression for checking email. if the user enters the wrong email address, an incorrect email format message is displayed immediately.

I want to use the same thing, but I don’t know the regular expression that checks the form input field if the user enters only spaces.

Please help me with such a regex? Thank,

+5
source share
2 answers
[RegularExpression(@"[^\s]+")]
public string Data { get; set; }
+10
source

:

^\S+$

.

()

, , , :

\S+
+3

All Articles