0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

フォームで空白文字を送信しない方法

Last updated at Posted at 2023-04-19

フォームで空白文字を送信しない方法

フォームに入力した文字をボタンのonClickで送信するときに、「空白文字が含まれる場合は送信できないようにする」方法。
(React/TypeScript)

<Button
  onClick={() => {
    if (todo.trim() !== '') {
      postTodo()
      setTodo('')
    }
  }}>
送信ボタン
</Button>

上記の例では、todo.trim() !== '' という条件式で、空白文字を含むTODOリストを送信しないようにしています。trim()は文字列の「先頭と末尾から空白文字を除去」するメソッド。
条件式の意味は、todoの「先頭と末尾から空白文字を除去」した結果が空文字列でなければ、TODOリストを送信する、ということ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?