Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="body" x-data="formValidation">
  <form class="form">
    <dl class="list">
      <div>
        <dt>
          <label for="name">お名前</label>
        </dt>
      <dd>
        <input type="text" id="name" x-model="name" x-on:blur="validateName">
        <p x-show="errors.name" x-text="errors.name"></p>
      </dd>
      </div>
      <div>
        <dt>入力した内容</dt>
        <dd x-show="name" x-text="name"></dd>
      </div>
    </dl>
    <dl class="list">
      <div>
              <dt>
        <label for="tel">電話番号</label>
      </dt>
      <dd>
        <input type="text" id="tel" x-model="tel" x-on:blur="validateTel">
        <p x-show="errors.tel" x-text="errors.tel"></p>
      </dd>
      </div>
      <div>
        <dt>入力した内容</dt>
        <dd x-show="tel" x-text="tel"></dd>
      </div>
    </dl>
    <dl class="list">
      <div>
              <dt>
        <label for="email">メールアドレス</label>
      </dt>
      <dd>
        <input type="text" id="email" x-model="email" x-on:blur="validateEmail">
        <p x-show="errors.email" x-text="errors.email"></p>
      </dd>
      </div>
      <div>
        <dt>入力した内容</dt>
        <dd x-show="email" x-text="email"></dd>
      </div>
    </dl>
  </form>
</div>
              
            
!

CSS

              
                .body {
  padding :50px 30px;
  background: #676767;

  .form {
    max-width: 1152px;
    margin: auto;
    background-color: white;
    padding: 40px;

    .list {
      &:not(:first-of-type) {
        margin-top: 30px;
      }
      > div {
      display: grid;
      grid-template-columns: 110px auto;
      }
      
      dd {
        p {
          font-weight: bold;
          color: red;
          margin: 0;
        }
      }
    }
  }
}
              
            
!

JS

              
                document.addEventListener('alpine:init', () => {
  Alpine.data('formValidation', () => ({
    name: '', // お名前
    tel: '', // 電話番号
    email: '', // メールアドレス
    errors: {}, // エラーメッセージ
    
    validateName() {
      if (!this.name) {
        this.errors.name = "※お名前は必須です。"
      } else {
        delete this.errors.name
      }
    },
    
    validateTel() {
      const telPattern = /^\d{2,4}-?\d{2,4}-?\d{4}$/
      if (!this.tel) {
        this.errors.tel = "※電話番号は必須です。"
      } else if (!telPattern.test(this.tel)) {
        this.errors.tel = "有効な電話番号を入力してください。"
      } else {
        delete this.errors.tel
      }
    },
    
    validateEmail() {
      const emailPattern = /.+@.+\..+/
      if (!this.email) {
        this.errors.email = "※メールアドレスは必須です"
      } else if (!emailPattern.test(this.email)) {
        this.errors.email = "メールアドレスの形式で入力してください"
      } else {
        delete this.errors.email
      }
    }
  }))
})

              
            
!
999px

Console