I store values in my web.config. These are settings for the website. For example the event min age limit is 16. I have created my own data annotations to do this and puull back the values using the configurationManager but now I want to create a client side version.
I have this at the moment in a ValidationExtender.js file.
jQuery.validator.addMethod("checkUserAgeOfEvent", function (value, element, param) {
var dob = $("#DateOfBirth").val();
//do validation here
if(ConvertToAge(dob) == minAge)
{
// do something
}
return true;
});
jQuery.validator.unobtrusive.adapters.addBool("checkUserAgeOfEvent");
My problem is that how do I include my value from the web.config or do I have to hard code the min age value? I want to try and avoid this.
I have created my own data annotations to do this
If you implemented the IClientValidatable
interface for your custom data annotation you could add custom values to the client as I have illustrated in the following post
.
Basically in your GetClientValidationRules
method you would pass this minAge
to the client:
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = this.ErrorMessage,
ValidationType = "userage",
};
rule.ValidationParameters.Add("minAge", ConfigurationManager.AppSettings["MinAge"]);
yield return rule;
}
which could be easily retrieved on the client:
jQuery.validator.addMethod(
"checkUserAgeOfEvent",
function (value, element, params) {
var dob = $("#DateOfBirth").val();
var minAge = params.minAge;
if(ConvertToAge(dob) == minAge) {
// do something
}
return true;
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.