Answer the question
In order to leave comments, you need to log in
Input validation (returns false to string). How to fix?
The onChange event hangs on the input, according to which the handleChangeField function is executed when we write something to the input.
handleChangeField function:
const handleChangeField = (event) => {
let value = event.target.value;
setTitle(InputValidator(value, maxSize));
}
const InputValidator = (value, maxSize) => {
let regExp = /[^а-яА-Яa-zA-Z0-9\s]/.test(value);
if (regExp) return false;
if (value.length > maxSize) return false;
return value;
}
export default InputValidator;
Answer the question
In order to leave comments, you need to log in
Let the validator return true/false instead of valid_value/false:
const validator = (value, maxSize) =>
!(/[^а-яА-Яa-zA-Z0-9\s]/.test(value) || value.length > maxSize);
if (validator(value, maxSize)) {
setTitle(value);
}
const validator = (defaultValue, value, maxSize) =>
(/[^а-яА-Яa-zA-Z0-9\s]/.test(value) || value.length > maxSize)
? defaultValue
: value;
setTitle(prevValue => validator(prevValue, value, maxSize));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question