0

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

1
  • It sounds like you want to reject inputs that consists of only whitespaces, but your error message that whitespaces aren't allowed at all. Which is it? Commented Mar 7, 2014 at 7:24

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.