Define Regular Expression Validation for custom DataType in MVC4

I upgraded a project from MVC3 to MVC4 and noticed that one of my validators wasn't triggering anymore. I chased it down to a custom DataType in the Shared/EditorTemplates folder that is used for capturing Social Security numbers in various forms across the site. (We only show the last 4 digits when the page is loaded after a save, to explain the regex.) SSN.cshtml

@{
    var ssn = (string)ViewData.TemplateInfo.FormattedModelValue;
    if (!String.IsNullOrWhiteSpace(ssn)){
        ssn = "###-##-" + Model.Substring(Model.Length - 4, 4);
    }
}
@Html.TextBox("", ssn, new { @class = "text-box single-line ssn", pattern = @"^(\d{3}|###)-(\d{2}|##)-\d{4}$", placeholder = "###-##-####", title = "Expected pattern is ###-##-####" })

It seems like in MVC4 the unobtrusive validation wants to look for data-val-regex-pattern as an attribute of the rendered text box, not just pattern. Has anyone ran into this before?

Note: I'd like to keep the validation on the custom data type so I don't have to always remember to add it to each model.

Thanks!

Answers


You can have your own custom validator and put it in the model property. here is an example

public class SocialSecurityAttribute : RegularExpressionAttribute
{
    public SocialSecurityAttribute () : base(@"^(\d{3}|###)-(\d{2}|##)-\d{4}$") { }
}

Then register it in the global asax.cs Application start as follows

  DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(SocialSecurityAttribute), typeof(RegularExpressionAttributeAdapter));

When you use it , it will be like any other built in attributes

[SocialSecurity(ErrorMessage ...)]
public string SocialSecurityNumber{get;set;}

I like implementing like this as the code can be reused . Otherwise you can just use RegularExpressionAttribute passsing the regex


I found out that in order to get MVC4 to render the correct data attributes to the input field, I'd need to replace the hyphens with underscores in the anonymous object passed to the TextBox helper. Here's the code that I ended up with:SSN.cshtml

@{
    var ssn = (string)ViewData.TemplateInfo.FormattedModelValue;
    if (!String.IsNullOrWhiteSpace(ssn) && ssn.Length == 11)
    {
        ssn = "###-##-" + Model.Substring(Model.Length - 4, 4);
    }
    else {
        ssn = "";
    }
}
@Html.TextBox("", ssn,
    new
    {
        @class = "text-box single-line ssn",
        data_val = "true",
        data_val_regex_pattern = @"^(\d{3}|###)-(\d{2}|##)-\d{4}$",
        data_val_regex = "Expected pattern is ###-##-####",
        placeholder = "###-##-####"
    }
)

Need Your Help

Corona SDK Director class error

android corona

I am developing a game in corona SDK, and I am using director class to change scene to scene and I get this error when i run the game on actual device but not in simulator.

Algorithm for allocating memory pages and page tables

c++ c linux algorithm memory-management

I want to design an algorithm for allocating and freeing memory pages and page tables. What data structures would allow best performance and simplest implementation?