Validator

Validator

Class for Validator Instance of this class working with Field instance.

Important! If you change any property onBlur, onChange or onFocus of validator, don't forget to reassign a validator to field again.

Constructor

new Validator(data)

Parameters:
Name Type Description
data iValidatorConstructor

(Optional) Object with parameters.

Source:
Example
let validator = new Validator({
    cb: (v:string) => {return v.length >= 4},
    message: "Minimal length is 4 characters",
    onChange: true
});
let input = new Input({
    title: "Input field",
    validator: validator,
});
validator.onChange(false);
input.validator(validator); // !IMPORTANT

Methods

cb(Callback) → {function}

Callback of validation. Providerd function here keep logic of validation

Parameters:
Name Type Description
Callback function | undefined

function or no-argument to get current value. Callback should return boolean value.

Source:
Example
let validator = new Validator({
    cb: (v:string) => {return v.length >= 4},
    message: "Minimal length is 4 characters"
 });
 validator.cb((v:string) => {return v.length <= 4});
 validator.message("Maximum length is 4 characters");

message(Message) → {string}

The message if value of field is invalid

Parameters:
Name Type Description
Message string | undefined

or no-argument to get current value.

Source:
Example
let validator = new Validator({
    cb: (v:string) => {return v.length >= 4},
    message: "Minimal length is 4 characters"
 });
 validator.cb((v:string) => {return v.length <= 4});
 validator.message("Maximum length is 4 characters");

onBlur(To) → {boolean}

To validate field when user leave current field

Parameters:
Name Type Description
To boolean | undefined

validate field when user leave current field if true field, or no-argument to get current value.

Source:
Example
let validator = new Validator({
    cb: (v:string) => {return v.length >= 4},
    message: "Minimal length is 4 characters",
    onBlur: true
 });
 validator.onBlur(false);

onChange(To) → {boolean}

To validate field each time when user press any key.

Parameters:
Name Type Description
To boolean | undefined

validate field when user press any key at current field if value is 'true', or no-argument to get current value.

Source:
Example
let validator = new Validator({
    cb: (v:string) => {return v.length >= 4},
    message: "Minimal length is 4 characters",
    onChange: true
 });
 validator.onChange(false);

onFocus(To) → {boolean}

To remove error effect (status-error and error message) from field, when user focused back on field.

Parameters:
Name Type Description
To boolean | undefined

remove error effect if it is 'true', or no-argument to get current value.

Source:
Example
let validator = new Validator({
    cb: (v:string) => {return v.length >= 4},
    message: "Minimal length is 4 characters",
    onFocus: true
 });
 validator.onFocus(false);

require(Is) → {boolean}

To skip validation if value of field is empty when it isn't required.

Parameters:
Name Type Description
Is boolean | undefined

required field or no-argument to get current value.

Source:
Example
let validator = new Validator({
    cb: (v:string) => {return v.length >= 4},
    message: "Minimal length is 4 characters",
    require: true
 });
 validator.require(false);

valid(witch) → {boolean}

To validate provided Field.

Parameters:
Name Type Description
witch iFieldClass

need to validate.

Source:
Example
let input = new Input({
     title: "Input field",
     validator: validator
 });
 validator.valid(input);