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="fieldset">
	<div class="legend">バリデーションフォーム</div>

	<div class="form-group">
		<div class="form-label"><label class="control-label required">お名前</label></div>
		<div class="form-control">
      <input type="text" name="sei" size="16" placeholder="山田" maxlength="16" value="">
      <input type="text" name="mei" size="16" placeholder="太郎" maxlength="16" value=""></div>
	</div>
  
  <div class="form-group">
		<div class="form-label"><label class="control-label required">電話番号</label></div>
		<div class="form-control"><input type="text" name="tel" size="20" placeholder="0312345678" maxlength="20" value="">
    <p class="help-block">10桁または11桁の数字を入力してください。(ハイフンは必要ありません)</p>
      </div>
    
	</div>
  

	
	<div class="form-group">
		<div class="form-label"><label class="control-label required">郵便番号</label></div>
		<div class="form-control">
			<input type="text" name="zip" size="10" placeholder="1234567" maxlength="10" value="">
			<p class="help-block">7桁の数字を入力してください。(ハイフンは必要ありません)</p>
		</div>
	</div>

              
            
!

CSS

              
                @charset "UTF-8";

/* ----入力フォーム共通---- */

input,
button,
select,
textarea {
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
}

input[type="text"],
input[type="tel"],
input[type="email"],
input[type="search"],
input[type="password"],
input[type="url"],
input[type="number"],
select,
textarea {
  padding: 4px;
  border: 1px solid #ccc;
  background-color: #fff;
  max-width: 43%;
  margin: 5px;
}

input[type="text"]:hover,
input[type="tel"]:hover,
input[type="email"]:hover,
input[type="search"]:hover,
input[type="password"]:hover,
input[type="url"]:hover,
input[type="number"]:hover,
select:hover,
textarea:hover {
  border: 1px solid #666;
}

input[type="radio"] {
  width: 18px;
  height: 18px;
  vertical-align: middle;
  margin-right: 5px;
}

input[type="checkbox"] {
  width: 18px;
  height: 18px;
  vertical-align: middle;
  margin-right: 5px;
}

label.radio,
label.checkbox,
input[type="radio"],
input[type="radio"] + label,
input[type="checkbox"],
input[type="checkbox"] + label,
select {
  cursor: pointer;
}

input[type="text"][disabled] {
  background: #dcdcdc;
  cursor: not-allowed;
}

/* ----入力フォームタイトル---- */

.legend {
  margin: 40px 0 20px;
  padding: 0 0 5px 2px;
  font-size: 26px;
  font-weight: normal;
  border-bottom: #999 1px solid;
}

/* ----縦型入力フォーム---- */

.fieldset-vertical {
  margin-top: 10px;
  margin-bottom: 10px;
}

.fieldset-vertical .form-group {
  margin: 5px 0 10px 0;
}

.fieldset-vertical .form-label,
.fieldset-vertical .constraint {
  margin: 0 0 5px 0;
}

.fieldset-vertical .form-label {
  padding: 10px;
  margin-right: 10px;
  text-align: left;
  background: #f9f9f9;
  vertical-align: middle;
}

.fieldset-vertical .form-label label {
  font-weight: bold;
}

.fieldset-vertical .form-control {
  clear: both;
}

/* ----横型入力フォーム---- */

.fieldset {
  margin-top: 10px;
  margin-bottom: 10px;
  width: 100%;
}

.fieldset .form-group {
  display: table;
  width: 100%;
  border-top: #e5e5e5 1px solid;
}

.fieldset .form-group:last-child {
  border-bottom: #e5e5e5 1px solid;
}

.fieldset .form-label,
.fieldset .constraint,
.fieldset .form-control {
  display: table-cell;
}

.fieldset .form-label {
  width: 23%;
  padding: 25px 15px 25px 15px;
  text-align: left;
  background: #f9f9f9;
}

.fieldset .form-label label {
  font-weight: bold;
}

.fieldset .constraint {
  width: 65px;
  padding: 5px;
  background: #f9f9f9;
}

.fieldset .form-control {
  padding: 5px 0 5px 20px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

/* ----入力支援---- */

.form-error {
  display: block;
  color: #a94442;
}

.required:after {
  display: inline-block;
  margin-left: 10px;
  padding: .2em .6em;
  font-size: 11px;
  text-align: center;
  white-space: nowrap;
  vertical-align: top;
  border-radius: .25em;
  color: #fff;
  background-color: #d9534f;
  content: "必須";
}

.help-block {
  display: block;
  width: 100%;
  font-size: 11px;
  margin-top: 5px;
  color: #737373;
}

.count-msg-another {
  display: block;
  font-size: 11px;
  margin-top: 5px;
  color: #555;
}

.count-msg {
  display: block;
  font-size: 11px;
  margin-top: 5px;
  color: #f66;
}

/* ----入力フォームボタン---- */

.action-buttons {
  margin-top: 10px;
  width: 100%;
  text-align: center;
  display: block;
}

.action {
  display: inline;
  margin-left: 10px;
  margin-right: 10px;
}

.action .btn {
  margin-top: 10px;
  padding: 8px 8px;
  font-size: 16px;
  border-bottom-width: 2px;
  min-width: 160px;
}

/* ----ラジオボタン、チェックボックス---- */

.radio {
  margin-right: 10px;
}

.checkbox {
  margin-right: 10px;
}

/* ----EFO---- */
.efo-icon {
  border: none;
  vertical-align: middle;
}

input[type="text"].efo-error,
input[type="tel"].efo-error,
input[type="email"].efo-error,
input[type="search"].efo-error,
input[type="password"].efo-error,
input[type="url"].efo-error,
input[type="number"].efo-error,
select.efo-error,
textarea.efo-error {
  background-color: #f2dede;
}

input[type="text"].efo-valid,
input[type="tel"].efo-valid,
input[type="email"].efo-valid,
input[type="search"].efo-valid,
input[type="password"].efo-valid,
input[type="url"].efo-valid,
input[type="number"].efo-valid,
select.efo-valid,
textarea.efo-valid {
  background-color: #e9f2d7;
}



              
            
!

JS

              
                class Validate {
  constructor() {
    this.isError = false
    this.errorMessage = ""
    this.errorMessageList = [
      "桁数に誤りがあります。",
      "半角数字で入力してください。",
    ]
  }
  // 未入力の判定
  checkEntered(formPartSelf) {
    const _this = this
    formPartSelf.on("keyup focus blur", function (e) {
      const targetElm = $(this)
      const targetValue = e.target.value

      if (e.type == "focus") {
        if (targetValue == "") {
          _this._validateReset(targetElm)
        }
      }

      if (e.type == "blur") {
        if (targetValue == "") {
          _this._validateError(targetElm)
          return
        }
      }

      if (e.type == "keyup") {
        if (targetValue == "") {
          _this._validateReset(targetElm)
          _this._balloonHide(targetElm)
        } else {
          _this._validateNormal(targetElm)
        }
      }
    })
  }
  // 数字の桁数チェック
  checkNumber(formPartSelf, minNum, maxNum) {
    const _this = this
    formPartSelf.on("keyup", function (e) {
      const targetElm = $(this)
      const targetValue = e.target.value

      // 未入力の場合は処理しない
      if (targetValue == "") return

      if (e.type == "keyup") {
        _this.isError = false
        _this._minLength(targetValue, minNum)
        _this._maxLength(targetValue, maxNum)
        _this._isNumber(targetValue)
      }
      _this._judgeShowError(targetElm)
    })
  }

  // 入力数チェック(最大)
  _maxLength(val, num) {
    const targetDigit = val.toString().length
    if (targetDigit > num) {
      this.isError = true
      this.errorMessage = this.errorMessageList[0]
    }
  }
  // 入力数チェック(最小)
  _minLength(val, num) {
    const targetDigit = val.toString().length
    if (targetDigit < num) {
      this.isError = true
      this.errorMessage = this.errorMessageList[0]
    }
  }
  // 数字チェック
  _isNumber(val) {
    if (isNaN(val)) {
      this.isError = true
      this.errorMessage = this.errorMessageList[1]
    }
  }

  // エラー判定
  _judgeShowError(targetElm) {
    if (this.isError) {
      this._validateError(targetElm)
      this._balloonShow(targetElm)
    } else {
      this._validateNormal(targetElm)
      this._balloonHide(targetElm)
    }
  }

  // ツールチップ表示
  _balloonShow(targetElm) {
    console.log(targetElm)
    const _this = this
    // 修正ここから
    if (targetElm?.data("balloon")?.data('active')) {
      targetElm.data("balloon").data('active', false);
    }
    targetElm
      .one("blur", function (e) {
        targetElm.hideBalloon()
      })
      .showBalloon({
        contents: _this.errorMessage,
      })
    // 修正ここまで
    
  }

  // ツールチップ非表示
  _balloonHide(_targetElm) {
    _targetElm.hideBalloon()
  }
  // エラー表示
  _validateError(targetElm) {
    targetElm.removeClass("efo-valid")
    targetElm.addClass("efo-error")
  }
  // 入力OK
  _validateNormal(targetElm) {
    targetElm.removeClass("efo-error")
    targetElm.addClass("efo-valid")
  }
  //  リセット
  _validateReset(targetElm) {
    targetElm.removeClass("efo-valid")
    targetElm.removeClass("efo-error")
    targetElm.hideBalloon()
  }
}

$(function () {
  // Inputにイベント登録
  $("input").each(function () {
    const formPartSelf = $(this)
    // autocompleteを無効化する
    formPartSelf.attr("autocomplete", "off")

    const validate = new Validate()

    // 未入力の判定
    validate.checkEntered(formPartSelf)

    // 電話番号
    if (formPartSelf.attr("name") == "tel") {
      validate.checkNumber(formPartSelf, 10, 11)
    }
    // 郵便番号
    if (formPartSelf.attr("name") == "zip") {
      validate.checkNumber(formPartSelf, 7, 7)
    }
  })
})

              
            
!
999px

Console