Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom RegularExpressionAttribute missing data-val-regex-pattern for client-side validation
    primarykey
    data
    text
    <p>I have created the following custom RegularExpressionAttribute</p> <pre><code>[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable { public AlphaNumericAttribute() : base("^[-A-Za-z0-9]+$") { } public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "alphanumeric" }; } } </code></pre> <p>The field in the ViewModel is decorated with my AlphaNumeric attribute:</p> <pre><code>[AlphaNumeric(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Resources.DriverLicenseNumber_RegexError_)] public string DriverLicenseNumber { get; set; } </code></pre> <p>The field is built in the view:</p> <pre><code>@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" })) { @Html.LabelFor(m =&gt; m.DriverLicenseNumber) @Html.ValidationMessageFor(m =&gt; m.DriverLicenseNumber) @Html.TextBoxFor(m =&gt; m.DriverLicenseNumber) } </code></pre> <p>This should yield the proper <em>"data-"</em> validation attributes on my html input tag. However, the rendered tag looks like this:</p> <pre><code>&lt;input data-val="true" data-val-alphanumeric="Please enter a valid driver's license number." id="DriverLicenseNumber" name="DriverLicenseNumber" type="text" value="" maxlength="20" class="valid"&gt; </code></pre> <p>Conspicuously absent are the <em>data-val-regex</em> and <em>data-val-regex-pattern</em> attributes that are supposed be rendered. </p> <p>I have built other validators with the exact same structure, and they work correctly, like this SSN validation which handles masked spaces for a masked input using jquery masking:</p> <pre><code>public class SsnAttribute : RegularExpressionAttribute, IClientValidatable { public SsnAttribute() : base("^([0-9]{3}–[0-9]{2}–[0-9]{4})|([ ]{3}–[ ]{2}–[ ]{4})|([0-9]{9,9})$") { } public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "ssn" }; } </code></pre> <p>}</p> <p>With the accompanying application on the ViewModel:</p> <pre><code>[Ssn(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Resources.SocialSecurity_RegexError_)] public new string SocialSecurityNumber { get; set; } </code></pre> <p>The field is built in the view:</p> <pre><code>@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" })) { @Html.LabelFor(m =&gt; m.SocialSecurityNumber) @Html.ValidationMessageFor(m =&gt; m.SocialSecurityNumber) @Html.TextBoxFor(m =&gt; m.SocialSecurityNumber) } </code></pre> <p>This validation attribute correctly renders the data-val-regex and data-val-regex-pattern attributes:</p> <pre><code>&lt;input class="SSNMask valid" data-val="true" data-val-regex="Please enter a valid social security number." data-val-regex-pattern="^([0-9]{3}–[0-9]{2}–[0-9]{4})|([ ]{3}–[ ]{2}–[ ]{4})|([0-9]{9,9})$" id="SocialSecurityNumber" name="SocialSecurityNumber" type="text" value="" maxlength="22"&gt; </code></pre> <p><br/><br/> I cannot figure out what I am missing with the AlphaNumeric attribute that it does not render the appropriate html attributes.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload