Jobi
Jobi

Reputation: 1122

Validate spaces using regular expression

I want to validate a field using regular expression as a data annotation. Suppose a user entered only spaces in a field need to alert a message something like "Spaces are not allowed". I'm using MVC4 and EF

Upvotes: 0

Views: 1214

Answers (2)

Suraj
Suraj

Reputation: 417

[RegularExpression(@"^[\S]*$", ErrorMessage = "White space found")]

and then probably in the view you can alert the user.

Upvotes: 1

aloisdg
aloisdg

Reputation: 23521

If this is for an HTML form, you can use simple HTML.

<form onsubmit="alert('checked');return false;">
    <input required pattern=".*\S+.*" title="This field is required">
    <input type="submit" value="check" />
</form>

Upvotes: 0

Related Questions