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 id="root"></div>
  
              
            
!

CSS

              
                .container {
  background: #eaeaea;
  padding:16px;
  max-width:680px;
  margin: 10px auto;
}
input {
  -webkit-tap-highlight-color: rgba(0,0,0,0.2);
  tap-highlight-color: rgba(0,0,0,0.2);
  transition: .3s;
  -moz-transition: .3s;
  -webkit-transition: .3s;
  -o-transition: .3s;
  -ms-transition: .3s;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: calc(100% - (0.6em * 2));
  display: block;
  padding: 0.6em;
  min-height: calc(1em * 1.75);
  border-radius: 8px;
  background: #fff;
  border-style: solid;
  border-width: 1px;
  border-color: #ccc;
}
.confirmation {
  border-bottom: 1px solid ;
  &_txt {
      font-weight:bold;
      margin: 0;
      min-height: calc(1em * 1.75);
  }
}
.item_name {
  display:flex;
 align-items: center;
  .require {
    font-size:.6em;
    color: #fff;
    background: red;
    padding:.3em .5em;
    margin-left: 5px;
  }
}
.submit_btn {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: 0;
  border-radius: 0;
  display: block;
  background: #ccc;
  width: 40%;
  padding: 10px;
  margin: 30px auto 0;
  text-align: center;
  font-size: 20px;
  &:hover {
    cursor: pointer;
  }
}
.error {
  display:block;
  color:red;
  padding-top:5px;
}
              
            
!

JS

              
                const ContentArea = () => {
  const [emailTxt, SetEmailTxt] = React.useState("");
  const [errorMessageAccount, SetErrorMessageAccount] = React.useState([]);
  const [errorMessageEmail, SetErrorMessageEmail] = React.useState([]);
  const [checkForm, SetcheckForm] = React.useState({
    account: "",
    email: ""
  });

  const errorTxt = {
    minTxt: "8文字以上で入力してください。",
    maxTxt: "12文字以内で入力してください。",
    alphabetTxt: "英数字で入力してください",
    emailTxt: "メールの形式で入力してください。"
  }
  
  const onChangAccount = (e)=> {
    const targetValue = e.target.value;
    let errorMessageAry = [];
    const pattern = /^[A-Za-z0-9]*$/;
    
    // 入力が空の場合
    if(targetValue == "") {
      SetErrorMessageAccount([]);
      SetcheckForm({
        ...checkForm,
        account: targetValue
      });
      return;
    }
    
    // 文字数のチェック
    if(targetValue.length < 8) {
      errorMessageAry.push(errorTxt.minTxt);
    } else if(targetValue.length > 12) {
      errorMessageAry.push(errorTxt.maxTxt);
    }
    
    // 英数字のチェック
    if (!pattern.test(targetValue)) {
      errorMessageAry.push(errorTxt.alphabetTxt);
    } 
 
    // console.log(validationAry);
    // console.log(Array.from(new Set(validationAry)));
    
    // エラーセット
    SetErrorMessageAccount(errorMessageAry);
   
    // Form入力チェック用の値更新
    SetcheckForm({
      ...checkForm,
      account: targetValue
    });
  }
  
  const onChangeEmail = (e) => {
    const targetValue = e.target.value;
    let errorMessageAry = [];
    const pattern = /^[A-Za-z0-9]{1}[A-Za-z0-9_.-]*@{1}[A-Za-z0-9_.-]+.[A-Za-z0-9]+$/;
    
    // 入力が空の場合
    if(targetValue == "") {
      SetErrorMessageEmail([]);
      SetEmailTxt("");
      SetcheckForm({
        ...checkForm,
        email: targetValue
      });
      return;
    }
    
    // メール形式の入力かチェック
    if (!pattern.test(targetValue)) {
      errorMessageAry.push(errorTxt.emailTxt);
    }
    
    // エラーセット
    SetErrorMessageEmail(errorMessageAry);
    
    // Form入力チェック用の値更新
    SetcheckForm({
      ...checkForm,
      email: targetValue
    });
    
    // 確認用テキスト表示
    SetEmailTxt(targetValue);
  }

  const onClickForm = ()=> {
    let resultTxt = "";
    for(let item in checkForm) {
      if(checkForm[item] == "") resultTxt += `${item}に値が入力されていません\n`;
    }
    resultTxt += errorMessageAccount.length > 0 ?
      `アカウント入力エラー:${errorMessageAccount}\n`: "";
    resultTxt += errorMessageEmail.length > 0 ?
      `メールアドレス入力エラー:${errorMessageEmail}\n`: "";
    
    if(resultTxt == "") resultTxt += "バリデーションチェックOKです!"
    alert(resultTxt);
  }

  return(
    <div className="container">
      <section>
        <p className="item_name">アカウント<span className="require">必須</span></p>
        <input type="text" onChange={onChangAccount} placeholder="8〜12文字の英数字で入力してください" />
     
        {errorMessageAccount.map((error, index)=> <span key={index} className="error">{error}</span>)}
        <p className="item_name">メールアドレス<span className="require">必須</span></p>
        <input type="text" value={emailTxt} onChange={onChangeEmail} />
        {errorMessageEmail.map((error, index)=> <span key={index} className="error">{error}</span>)}
        
        <div className="confirmation">
          <p>確認:</p>
          <p className="confirmation_txt">{emailTxt}</p>
        </div>
        
        <button onClick={onClickForm} className="submit_btn">
          登録
        </button>
      </section>
    </div>
  )
}


ReactDOM.render(
    <ContentArea />,
  document.getElementById('root')
);
              
            
!
999px

Console