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

              
                
<form id="myForm" method="post" action="#">
  <table>
    <tbody>
      <tr>
        <th>電話番号<span class="required">必須</span></th>
        <td>
          <input name="tel" type="tel" value="">
          <div id="telErr"></div>
        </td>
      </tr>
      <tr>
        <th>フリガナ<span class="required">必須</span></th>
        <td>
          <input name="furigana" type="text" value="">
          <div id="furiganaErr"></div>
        </td>
      </tr>
    </tbody>
  </table>
  <button>送信</button>
</form>
              
            
!

CSS

              
                section {
  width: 92%;
  height: auto;
  max-width: 1000px;
  margin: 20px auto 0 auto;
  padding: 0 4%;
  color: #555;
}

table {
  display: table;
  width: 100%;
  border-collapse: collapse;
}

table tbody tr {
  border-top: 1px solid #aaa;
  border-bottom: 1px solid #aaa;
  
}

table tbody tr th {
  width: 30%;
  padding: 20px;
  text-align: left;
  vertical-align: top;
  border-right: 1px solid #aaa;
}

table tbody tr th .required {
  display: inline-block;
  margin: 0 0 0 10px;
  padding: 0 5px;
  color: #fff;
  font-size: 0.7em;
  border-radius: 25px;
  background-color: red;
  vertical-align: middle;
}

table tbody tr td {
  width: 70%;
  padding: 20px;
}

table tbody tr td .part {
  display: inline-block;
  width: 20%;
}

table tbody tr td label.error {
  display: block;
  width: 100%;
  margin: 5px 0 0 0;
  color: red;
  font-size: 0.9em;
  font-weight: bolder;
}

table tbody tr td input[type="tel"],
table tbody tr td input[type="text"],
table tbody tr td input[type="email"]{
  width: 100%;
  box-sizing: border-box;
  border: 1px solid #aaa;
}

table tbody tr td textarea {
  width: 100%;
  height: 100px;
  box-sizing: border-box;
  border: 1px solid #aaa;
}

form button {
  display: block;
  width: 200px;
  height: 50px;
  margin: 20px auto 0 auto;;
  border: 1px solid #aaa;
  background-color: #fff;
  cursor: pointer;
}
              
            
!

JS

              
                //独自の検証ルールを設定
var methods = {
  tel: function(value, element){
    return this.optional(element) || /^(0[5-9]0[0-9]{8}|0[1-9][1-9][0-9]{7})$/.test(value.replace(/[━.*‐.*―.*-.*\-.*ー.*\-]/gi,''));
  },
  katakana: function(value, element){
    return this.optional(element) || /^[ア-ン゛゜ァ-ォャ-ョー「」、]+$/.test(value);
  }
};

//検証ルールを有効化
var rules = {
  tel: {
    tel: true,
    required: true
  },
  furigana: {
    katakana: true,
    required: true
  }
};

//各ルールのエラーメッセージを設定
var messages = {
  tel: {
    tel: '電話番号を正確にご入力ください',
    required: '電話番号をご入力ください'
  },
  furigana: {
    katakana: '全角カタカナでご入力ください',
    required: 'フリガナをご入力ください'
  }
};

//独自の検証ルールを追加
$.each(methods, function(key) {
  $.validator.addMethod(key, this);
});

//フォームに検証ルールを設定
$(function(){
  $('#myForm').validate({
    rules: rules,
    messages: messages,

    //エラーメッセージ出力箇所設定
    errorPlacement: function(error, element){

      if(element.attr("name") === "furigana") {
        error.insertAfter("#furiganaErr" );

      } else if(element.attr("name") === "tel") {
        error.insertAfter("#telErr" );

      } else {
        error.insertAfter(element);
      }
    }
  });
});
              
            
!
999px

Console