0

Situation

I have a social security field that has an input mask on it so that we get this: "999-99-9999". When the form is submitted, the value of this field is "unmasked", meaning the hyphens are removed.

Problem

My project is written in EF code-first, and I would like to specify the maximum string length to be 9 IN THE DATABASE ONLY!! I already have client-side validation in the form of a regular expression that allows up to 11 characters. When the form is submitted, and the hyphens are removed, the value will then be 9 and everything will be hunky dory.

Question

What data annotation(s) should I use to set the max string length of the field in the database only, while at the same time specifying a different max-length for client-side validation?

Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48
  • Why not store it as an integer? – TnTinMn Feb 04 '16 at 03:16
  • I would have to do a custom input field for it. I don't think any of the validation would work very well if I tried to set up a custom editor for an integer but specify validation for a string. Because it will be a string due to the hyphens. Dunno how to get around that. – Ross Brasseaux Feb 04 '16 at 03:18
  • 1
    What about a regex that allows both - `^\d{3}-\d{2}-\d{4}|\d{9}$` –  Feb 04 '16 at 03:19
  • I am currently applying the regex `^\d\d\d(?:\d\d|-\d\d-)\d\d\d\d$` – Ross Brasseaux Feb 04 '16 at 03:21
  • Use `MaxLength` attribute... See here **https://msdn.microsoft.com/en-us/data/jj591583#MaxMin** – Trevor Feb 04 '16 at 03:41
  • @Codexer yes, I've tried max length. Using it will apply the client-side validation. – Ross Brasseaux Feb 04 '16 at 03:42
  • And StringLength?... – Trevor Feb 04 '16 at 03:46
  • If that doesn't work either write a custom field or change directions of the mask... There's nothing more I'm affraid unless there's something I haven't seen. – Trevor Feb 04 '16 at 04:04
  • Also why add the hyphens if it's not what you are storing...? Is it just for looks? – Trevor Feb 04 '16 at 04:08
  • @Codexer Of course. Just wanted it to be as easy as possible for the user, and I wound up spending a couple hours trying to solve this puzzle. I think I found the answer—I'll post it shortly. – Ross Brasseaux Feb 04 '16 at 04:10

1 Answers1

0

I found the correct answer here. You can disable client-side validation by changing the corresponding HTML attribute in the editor control (e.g., the @Html.EditorFor(), @Html.TextboxFor(), etc.). See below for an example:

Using the code-first example above, we will want to create a field for social security numbers. In this field, we want to client-side validation to only use our regular expression, we want the field to be required, and we want the data-type in the database to be nvarchar(9). Here is my working code:


Model

<Display(Name:="Social Security Number", ShortName:="SSN", Description:="Employee social security number."),
            RegularExpression("^\d\d\d(?:\d\d|-\d\d-)\d\d\d\d$", ErrorMessage:="Please enter a valid social security number."),
            StringLength(11, MinimumLength:=9, ErrorMessage:="The social security number must be no more than 9 characters long."),
            MaxLength(9)>
        Public Property ssn As String

View (razor syntax)

<div class="form-group">
     @Html.LabelFor(Function(model) model.ssn, htmlAttributes:=New With {.class = "control-label col-md-2"})
     <div class="col-md-10">
         @Html.EditorFor(Function(model) model.ssn, New With {.htmlAttributes = New With {.class = "form-control", .data_val_maxlength_max = "11"}})
         @Html.ValidationMessageFor(Function(model) model.ssn, "", New With {.class = "text-danger"})
     </div>
 </div>

Remarks

  • Many of the validations can be turned off completely, you just have to find the right HTML attribute with a boolean value and set it to false (e.g., data_val="false")
  • The underscores in my HTML attribute names are converted to hyphens automatically by ASP when the markup is generated.
  • The data_val_maxlength_max attribute (i.e., the data-val-maxlength-max attribute) is the name of the HTML attribute that corresponds to the MaxLength(<integer>) data annotation in the model. In my view code, I change it from 9 to 11 on the client-side (and only on the client-side).
Community
  • 1
  • 1
Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48