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 3 years have passed since last update.

HTML5 inputタグ 入力検証 まとめ

Posted at
<!doctype html>
<html lang="ja">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    </head>
    <body>
        <div class="container mt-5">
            <h1 class="display-4 text-center">入力検証</h1>
            <div class="register">
                <form class="test needs-validation" novalidate method="post">

                    <!--メール-->
                    <div class="form-group position-relative" style="margin-bottom:2rem;">
                        <label for="email" class="te">メール形式</label>
                        <input type="email" Name="Email" id="email" class="form-control" maxlength="255" required placeholder="abcd@aaa.com">
                        <div class="invalid-tooltip">
                            必須項目 メール形式
                        </div>
                    </div>

                    <!--数値(0開始OK)-->
                    <div class="form-group position-relative" style="margin-bottom:2rem;">
                        <label for="number">数値</label>
                        <input type="tel" Name="Number" id="number" class="form-control" minlength="8" maxlength="13" pattern="^[0-9]+$" required placeholder="0123456789">
                        <div class="invalid-tooltip">
                            必須項目 数値のみ 8桁以上 13桁以下
                        </div>
                    </div>

                    <!--数値(自然数のみ)-->
                    <div class="form-group position-relative" style="margin-bottom:2rem;">
                        <label for="naturalNumber">自然数</label>
                        <input type="tel" Name="NaturalNumber" id="naturalNumber" class="form-control" minlength="8" maxlength="13" pattern="^[1-9][0-9]*$" required placeholder="123456789">
                        <div class="invalid-tooltip">
                            必須項目 自然数のみ 8桁以上 13桁以下
                        </div>
                    </div>


                    <!--テキスト-->
                    <div class="form-group position-relative" style="margin-bottom:2rem;">
                        <label for="name">テキスト</label>
                        <input type="text" name="Name" id="name" class="form-control" maxlength="20" pattern=".*\S+.*" required placeholder="山田花子">
                        <div class="invalid-tooltip">
                            必須項目 8文字以下 空白のみ禁止
                        </div>
                    </div>

                    <!--password-->
                    <div class="form-group position-relative" style="margin-bottom:2rem;">
                        <label for="password">パスワード(半角英数字)</label>
                        <input type="password" name="Password" id="password" class="form-control" maxlength="20" pattern="^[0-9A-Za-z]+$" required placeholder="パスワードを入力">
                        <div class="invalid-tooltip">
                            必須項目 半角英数字のみ 20文字以下 空白のみ禁止
                        </div>
                    </div>


                    <button type="submit" class="btn btn-primary btn-block mt-5">Submit</button>
                </form>
                <!--Scripts-->
                <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
                <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
                <script>
                    (function () {
                        'use strict';
                        window.addEventListener('load', function () {
                            // Fetch all the forms we want to apply custom Bootstrap validation styles to
                            var forms = document.getElementsByClassName('needs-validation');
                            // Loop over them and prevent submission
                            var validation = Array.prototype.filter.call(forms, function (form) {
                                form.addEventListener('submit', function (event) {
                                    if (form.checkValidity() === false) {
                                        event.preventDefault();
                                        event.stopPropagation();
                                    }
                                    form.classList.add('was-validated');
                                }, false);
                            });
                        }, false);
                    })();
                </script>
            </div>
        </div>
    </body>
</html>
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?