maztt
maztt

Reputation: 12294

asp.net mvc regular expression enter

^[a-zA-Z0-9-_. ]*$ for textarea

I want to make available the enter as well in the following regular expression. How would I do that?

Upvotes: 3

Views: 893

Answers (2)

Robert Greiner
Robert Greiner

Reputation: 29722

You can use declarative attributes to accomplish this.

Here's a post to get you started.

Using your provided regular expression, you could do the following in your model code to make a field both required and validated against a specified criteria.

[Required(ErrorMessage = "Please enter a value")]
[RegularExpression("^[a-zA-Z0-9-_. ]$", ErrorMessage = "Please enter valid text")]
public string MyTextBox { get; set; }

If you are interested in learning more about attributes (not necessarily with respect to ASP .NET MVC) you can check out this article for further reading.

Upvotes: 0

LukeH
LukeH

Reputation: 269348

Do you mean that you want to allow newline characters? That would be:

^[a-zA-Z0-9-_. \r\n]*$

Upvotes: 3

Related Questions