UNPKG

@seges/angular-validators

Version:

TypeScript 1.8.10

41 lines (34 loc) 1.38 kB
import { IValidator } from "../IValidator"; export interface IMaxValueAttrs extends ng.IAttributes { maxValue: string; } export class MaxValueValidator implements IValidator<void> { /** * Function to use outside angular validation. * @param value The value to check. * @param compareValue The maximum allowed value. */ static validate(value: number, compareValue: number): boolean { if (!value || !compareValue) { return false; } return value <= compareValue; } /** * Validate if one value (number) is greater than other * Return true if value is greater than compare value. Otherwise return false * @param value: Value to check * @param attrs: ng.IAttributes */ isValid(value: string, attrs?: IMaxValueAttrs): boolean { if (!value || !attrs.maxValue) { return false; } // The toString() is invoked to ensure it is actually a string we manipulate. // Input elements in IE pass values as strings while the model can pass values as numbers. // hence this workaround is required. let floatValue = value.toString().replace(",", "."); let floatMaxValue = attrs.maxValue.replace(",", "."); return parseFloat(floatValue) <= parseFloat(floatMaxValue); } }