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?

10
  • Why not store it as an integer? Commented Feb 4, 2016 at 3: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. Commented Feb 4, 2016 at 3:18
  • 1
    What about a regex that allows both - ^\d{3}-\d{2}-\d{4}|\d{9}$ Commented Feb 4, 2016 at 3:19
  • I am currently applying the regex ^\d\d\d(?:\d\d|-\d\d-)\d\d\d\d$ Commented Feb 4, 2016 at 3:21
  • Use MaxLength attribute... See here msdn.microsoft.com/en-us/data/jj591583#MaxMin Commented Feb 4, 2016 at 3:41

1 Answer 1

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).
Sign up to request clarification or add additional context in comments.

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.