Created
May 18, 2015 13:03
-
-
Save Lionhunter3k/67e54a23b422c1a5dc85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery.validator.addMethod('priceonrange', function (value, element, params) { | |
| var minPrice = parseFloat(params['minprice'], 10); | |
| var maxPrice = parseFloat(params['maxprice'], 10); | |
| var parsedValue = parseFloat(value, 10); | |
| if (parsedValue > maxPrice || parsedValue < minPrice) { | |
| return false; | |
| } | |
| return true; | |
| }); | |
| var setValidationValues = function (options, ruleName, value) { | |
| options.rules[ruleName] = value; | |
| if (options.message) { | |
| options.messages[ruleName] = options.message; | |
| } | |
| }; | |
| var $Unob = $.validator.unobtrusive; | |
| $.validator.unobtrusive.adapters.add("priceonrange", ["minprice", "maxprice"], function (options) { | |
| var value = { | |
| minprice: options.params.minprice, | |
| maxprice: options.params.maxprice | |
| }; | |
| setValidationValues(options, "priceonrange", value); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web; | |
| using System.ComponentModel.DataAnnotations; | |
| using System.Web.Mvc; | |
| namespace Mvc | |
| { | |
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] | |
| public class PriceValidationAttribute : ValidationAttribute, IClientValidatable | |
| { | |
| private decimal minPrice = 0.01M; | |
| private decimal maxPrice = 100.00M; | |
| public PriceValidationAttribute(double min,double max) | |
| { | |
| minPrice = Convert.ToDecimal(min); | |
| maxPrice = Convert.ToDecimal(max); | |
| } | |
| public override bool IsValid(object value) | |
| { | |
| var price = (decimal?)value; | |
| if (price.HasValue) | |
| { | |
| if (price < this.minPrice || price > this.maxPrice) | |
| return false; | |
| } | |
| return true; | |
| } | |
| public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) | |
| { | |
| var rule = new ModelClientPriceRangeValidationRule(base.ErrorMessage, this.minPrice, this.maxPrice); | |
| yield return rule; | |
| } | |
| } | |
| public class ModelClientPriceRangeValidationRule : ModelClientValidationRule | |
| { | |
| public ModelClientPriceRangeValidationRule(string errorMessage, decimal minPrice, decimal maxPrice) | |
| { | |
| ErrorMessage = errorMessage; | |
| ValidationType = "priceonrange"; | |
| ValidationParameters.Add("minprice", minPrice); | |
| ValidationParameters.Add("maxprice", maxPrice); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment