Is it possible to disable client-side validation for a single model property in MVC

I have an MVC 4 model and I am creating html from it in the view using @ Html.TextBoxFor. In the model for one of the fields, I have the RegularExpression attribute, which is defined as follows:

[Required(ErrorMessageResourceType=typeof(ResourceFile), ErrorMessageResourceName="ResourceName1"
[RegularExpression(@"\w{3,5}", ErrorMessageResourceType=typeof(ResourceFile), ErrorMessageResourceName="ResourceName2")]
public string TestProperty { get; set; }

Note that the expression is more complex than that, but what I have here is suitable for testing. I set up unobtrusive client-side validation as described here: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

The problem is that I need to accept Russian characters .. .Net regular expressions have different meanings for \ w regular expressions for javascript, and as such, server-side validation works as I expect, but not on the client side.

Is it possible to disable client-side validation for the RegularExpression attribute without disabling it for the Required attribute?

Otherwise, can you simply disable client-side validation for this single property without disabling all other properties of this model object?

+4
source share
1 answer

. RegularExpressionAttribute, IClientValidatable. . IClientValidatable . - , , , RegularExpressionAttribute.

public class ServerSideOnlyRegularExpressionAttribute : RegularExpressionAttribute
{
    public ServerSideOnlyRegularExpressionAttribute(string pattern)
        : base(pattern)
    {
    }
}

[Required(ErrorMessageResourceType=typeof(ResourceFile), ErrorMessageResourceName="ResourceName1"
[ServerSideOnlyRegularExpression(@"\w{3,5}", ErrorMessageResourceType=typeof(ResourceFile), ErrorMessageResourceName="ResourceName2")]
public string TestProperty { get; set; }
+6

Source: https://habr.com/ru/post/1544782/


All Articles