UNPKG

@react-input-validator/rules

Version:

The validation rule objects used by the packages: `@react-input-validator/core`, `@react-input-validator/native` and `@react-input-validator/web`

29 lines (28 loc) 1.02 kB
import messages from './messages'; import ValidationRule from './ValidationRule'; const regex = /^(\+|-)?(\d+(\.\d+)?|\.\d+)$/; export class Numeric extends ValidationRule { static get regex() { return regex; } get errorMessage() { return this.lang(messages.numeric); } get resultValue() { return parseFloat(this.value); } validate() { this.isValid = regex.test(this.value); return this; } } export const numeric = new Numeric(); numeric.arrayAsSingle = function () { throw new Error("`numeric` rule object is shared among inputs. If you want to invoke `arrayAsSingle`, use `new Numeric()` instead."); }; numeric.setMessageFunc = function () { throw new Error("`numeric` rule object is shared among inputs. If you want to set message, use `new Numeric()` instead."); }; numeric.setPriority = function () { throw new Error("`numeric` rule object is shared among inputs. If you want to set priority, use `new Numeric()` instead."); };