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.

inputの属性覚え直し

Posted at

こんにちは!最近、セキュリティの本を読んでいた時にhtmlの属性が紹介されました。便利な機能があったことに気づき、自分なりにまとめてみたfいと思いましたので、今回はその内容をご紹介したいと思います。

コード

<input
  type='password'
  name='password'
  required
  pattern='^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$'
  title='パスワードには8文字以上の英数字を入力'
  autocomplete='new-password'
  inputmode='text'
/>

id, type, nameはお馴染みなので説明しません。

required

必須項目になる

スクリーンショット 2023-03-27 18.12.45.png

pattern

ブラウザがバリデーションしてくれる。

スクリーンショット 2023-03-27 18.14.03.png

<input 
  pattern='^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$'
/>

ちなみにこのバリデーションは任意の位置に数字と英字の両方が含まれかつ8文字以上という意味。
?= : 任意の位置
.\d : 数字が含まれる
.
[A-Za-z] : 英字
{8,} : 8文字以上

title

patternのメッセージ
スクリーンショット 2023-03-27 18.21.19.png

autocomplete

passwordを自動補完してくれます
autocomplete='new-password'に指定すれば自動入力を停止できたりします。
指定しない場合はname属性から判断してるみたいですね。nameと一緒なら意図的に書くことは少ないかも?詳しくはmdn見てください。

inputmode

スマホで見た時のキーボードを指定できます。デフォルトはtextです。

inputmode='numeric'
IMG_4005.jpg

inputmode='search'
IMG_4006.jpg

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?