01 Mar 2016
Let’s dive right in, plain and simple: for some reason, you need to make sure that your users don’t enter any non-printable characters into a text input field on a form. Maybe the data needs to travel to far reaches of the known universe, to a remote ancient banking backend system that runs on COBOL and mainframes (fueled by the energy of a dying brown dwarf). Or maybe the column in the database where this data is going to end up has never heard of Unicode. Or maybe that’s just one of those unexplainable, irrational so-called “business rules”, that some aspiring manager somewhere up in the ranks decided to implement.
ASP.NET MVC comes with a plenty of inbuilt validators, and RegularExpression
is one of them. I
will spare you the lengthy details of how validation works in ASP.NET MVC, if you’re interested
in the theory, check out this article.
All we need to do is to chuck a [RegularExpression]
validation attribute on our model and specify
a couple of parameters:
The value parameter for the regular expression attribute deserves a bit of explanation. If you look
at the ASCII table, you will see that printable part of the table starts with the space
character (
) and ends with the tilde character, that is ~
.
We’re using the character class regex in here, specifying the range of acceptable characters
starting from space and ending with the tilde: [ -~]
. Dash character serves here as a separator,
essentially saying: “match all characters starting from space and ending with tilde”. In order to
match more than just one character, we specify +
, which is a repetition operator.
There’s, at least, one more way of specifying the filtering regex for the [RegularExpression]
attribute - instead of including only printable characters, you can try to exclude non-printable
ones. So your regular expression would look something like this: [^\x00-\x7F]+
. This is actually
quite a bad idea if you are ever going to use FluentValidation. When this regex is translated to
JavaScript statement and executed, it’s going to produce the following error:
Uncaught SyntaxError: Invalid regular expression: /[^�-]/: Range out of order in character class
So instead of filtering out non-printable characters explicitly, just stick to checking that all characters are, in fact, printable.
That’s really it. What’s remaining is to see how this is getting validated in the controller.
If you want to add client-side JavaScript validation, refer to my earlier article, “How to use Bootstrap 3 validation states with ASP.NET MVC Forms”.
Download fully tested and 100% working Visual Studio solution with the source code used in this article for FREE – just enter your name and email in the form below, and I’ll send you the download link right away.
You will also get access to all the source code for all existing and new articles on the site, and access to my mailing list, which receives handy timesaving tips on .NET Core programming.