ASP.NET MVC - Dynamic RegEx Validator - asp.net-mvc

In our application our customers are allowed to define a couple of reference fields, and the validation for these reference fields by way of a regular expression. The Validation Regex is saved into our database.
What is the best way to apply this regex as validation for an MVC text box field?

In addition to checking server-side when the value's submitted, you can use remote validation which would create a round-trip to test the current value against logic stored on the server (or in this case in a database).
Have a look at the RemoteAttribute and that MSDN article above (which includes an example).

Related

Data annotation validation for multiple model properties

I'm currently using MVC data annotations for validating my form and it looks quite easy.
Now, I have a particular situation:
IF(checkbox1value = checked), then validate if textbox is not empty. If textbox is empty, then show validation error message.
How do I do this? Both checkbox and textbox are available as my model properties (bool and string respectively).
Is it possible to do this via a custom validation?
You can use a custom validator or IValidateableOject
There's a good explanation of both options here.
From the link.
If you need to support client validation, then a custom validator is what you want.
If your validation is strictly server side and you want to validate a number of business rules at once then use IValidateableObject.
I had to switch to Foolproof validation, which offered this functionality through it's RequiredIfTrue validator. I also really liked Fluent Validation since it was really flexible, and would also easily offer this functionality. Foolproof client-side support is provided for each of it's validators, where the boundary between client and server validation with Fluent Validation is not as easily distinguishable.

What advantage has Fluent Validation vs the default .NET Data Annotations

What advantage does the FluentValidation library have over the .NET System.ComponentModel.DataAnnotations?
Does it offer more flexible validation as it is not annotated (static field validation) on the property like validating a property depending on another's property value?
I'm not sure about data annotations but we've used FluentValidation for validation part in business logic. And easy integrating with ASP.NET MVC is nice bonus :)
It supports a lot of built-in rules, error messages localization, using object data in error messages, custom validation methods, conditional validation - applying some rules if object data matches a condition, rule sets - apply named set of rules, validation of aggregated objects and collections - and property names would be compatible with ASP.NET MVC property names and so on.

Mvc3 a potentially dangerous request.form ... Solution without disabling Validation

I'd rather not disable validation on my form. I feel like I'm not taking advantage of MVC's benefits if I do that. Is there a way to regex the contents of a property submitted in a form before mvc gets its hands on it? I simply want to allow only alphanumerical values and some symbols but still leave protection on. Do I have to disable with [ValidateInput(false)]??
You can create custom validation attributes and apply them to your entire model class, or individual properties.
http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx

How do I override the default client validation logic for numeric types in ASP.NET MVC 3?

I have a situation in which a number must be in the format R$ 3.000,00 which is not compatible with the default validation logic for numeric types in the client unobstrusive validation.
I need a way to implement my own JavaScript function and override the default one.
How do I do that?
There are specific locale files to download for your locality.
http://msdn.microsoft.com/en-us/library/gg674880(v=vs.98).aspx
There is also a good article here
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

Can we set ValidatInput(false) for only one element?

It's easy to set our action not to validate the input just hooking up the attribute like
[HttpPost, ValidateInput(false)]
public ActionResult Signup(SignupModel model)
{
...
}
But I was wondering, is there a way to only do this in just one form field? and not all of them in the actual <form>
or, I have to use this and then worry about encoding properly all other fields?
You should not rely on ValidateInput to 'encode' your values. It doesn't encode values - it just rejects some values outright. And even if you use it, you still must encode all your values that are user-entered and displayed on the site in any way.
In fact, because of that - I've never used that validation myself. For example, if all I did was rely on that validation, people would not be able to enter some very basic things in forms like this one, such as trying to show example HTML.
But even the MSDN documentation and every book I've read said that the ASP.NET validation there does not protect you against every possible malicious input. So essentially, you can not rely on it to protect your users. You still must encode values you are displaying (and you should encode them only as you are displaying them, otherwise you'll end up with all sorts of display bugs where you've double-encoded things)
Using MVC3 , the attribute can be applied to Model by specifying SkipRequestValidation.

Resources