I have a function onChange , where I write down what I put in the input in the state

 onChange = id => currentValue => { this.setState(prevState => ({ data: { ...prevState.data, [id]: currentValue } })); }; 

but there is a situation when first a space or several spaces are entered into the input. I need to ignore the spaces in the beginning and remove them. At the same time, so that this behavior is displayed on the onChange . That is, they made a focus on the input, entered several spaces, began to write, for example, an email and the cursor at that moment left at the beginning, as if there were no spaces

  • Well, don't update the state of input if value is not valid ... - xFloooo
  • This option will not work. then it will not be possible to write invalid data. and I want to be able to write invalid data in the input. but if suddenly spaces are at the beginning, then the cursor moves at the beginning here is a similar behavior on the form in the email field tinkoff.ru/cards/credit-cards/s7-airlines/… - werty
  • Well, then you need to check before updating the state whether there are at the beginning of the line "not whitespace", if they are not present, then render as it is (there will be only spaces), as soon as the visible characters appear, immediately cut out all spaces, first lines and output the line - xFloooo

1 answer 1

Try trim () . This method allows you to delete spaces, tabs at the beginning and at the end of a line. str.trim()