@seges/angular-validators
Version:
TypeScript 1.8.10
41 lines (34 loc) • 1.38 kB
text/typescript
import { IValidator } from "../IValidator";
export interface IMinValueAttrs extends ng.IAttributes {
minValue: string;
}
export class MinValueValidator implements IValidator<void> {
/**
* Function to use outside angular validation.
* @param value The value to check.
* @param compareValue The minimum 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?: IMinValueAttrs ): boolean {
if (!value || !attrs.minValue) {
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 floatMinValue = attrs.minValue.replace(",", ".");
return parseFloat(floatValue) >= parseFloat(floatMinValue);
}
}