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
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
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
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.
I hope this article helped you to move forward in your project work.
You can probably recall a time when you were stuck on some problem and the solution seemed so close, yet it took hours to figure it out. And when you eventually did that, it was something so infuriatingly stupid, you might have wanted to punch the monitor.
Do you want to avoid wasting your time on stupid bugs and traps in .NET? Subscribe to my mailing list and save HOURS of your life - I never spam, and I only send useful and actionable advice.