@seges/angular-validators
Version:
TypeScript 1.8.10
33 lines (27 loc) • 1.12 kB
text/typescript
import { IValidator } from "../IValidator";
export interface IDecimalsAttrs extends ng.IAttributes {
decimals: number;
}
export class DecimalsValidator implements IValidator<void> {
/**
* 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?: IDecimalsAttrs): boolean {
if (!value || !attrs.decimals) {
return true;
}
// 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(",", ".");
floatValue.toString().split(".");
let decimals = floatValue.toString().split(".")[1];
if (!decimals || !decimals.length) {
return true;
}
return decimals.length <= attrs.decimals;
}
}