検証

このサンプルではツアーコンポーネントの各ステップで入力内容の検証や、検証結果に応じるメッセージの表示を確認することができます。

ツアーコンポーネント(GcTour)は、各ステップで入力内容の検証を行い、検証結果に応じてメッセージの表示や「次へ」ボタンの活性・非活性の制御などを設定できます。 検証 ツアーコンポーネント(GcTour)の各ステップには、検証機能を設定できます。 検証に失敗した場合は、messageプロパティを使ってツールチップ内にエラーメッセージを表示し、disableNextButtonプロパティで「次へ」ボタンを無効化できます。 検証に成功した場合は、メッセージをクリアし、「次へ」ボタンを有効化することで、ユーザーがスムーズに次のステップへ進めるようになります。
import '@mescius/inputman/CSS/gc.inputman-js.css'; import { InputMan } from '@mescius/inputman'; InputMan.appearanceStyle = InputMan.AppearanceStyle.Modern; const username = new InputMan.GcTextBox(document.getElementById('username'), { watermarkDisplayNullText: '例)入力太郎', exitOnEnterKey: InputMan.ExitKey.Both, width: 250, height: 32, }); const mail = new InputMan.GcMask(document.getElementById('mail'), { exitOnEnterKey: InputMan.ExitKey.Both, width: 250, height: 32, }); const phone = new InputMan.GcMask(document.getElementById('phone'), { formatPattern: '\\D{2,3}-\\D{2,3}-\\D{4}', exitOnEnterKey: GC.InputMan.ExitKey.Both, width: 250, height: 32, }); const password1 = new InputMan.GcTextBox(document.getElementById('password1'), { useSystemPasswordChar: true, exitOnEnterKey: GC.InputMan.ExitKey.Both, width: 250, height: 32, }); const password2 = new InputMan.GcTextBox(document.getElementById('password2'), { useSystemPasswordChar: true, exitOnEnterKey: GC.InputMan.ExitKey.Both, width: 250, height: 32, }); const validator = new InputMan.GcValidator({ items: [ { control: username, ruleSet: [ { rule: InputMan.ValidateType.Required, failMessage: '必須項目です。', }, ], validateWhen: InputMan.ValidateWhen.Typing, }, { control: mail, ruleSet: [ { rule: InputMan.ValidateType.Required, failMessage: '必須項目です。', }, { rule: (control) => { const regexp = /^[A-Za-z0-9]{1}[A-Za-z0-9_.-]*@{1}[A-Za-z0-9_.-]{1,}\.[A-Za-z0-9]{1,}$/; return regexp.test(control.value); }, failMessage: '正しいメールアドレス形式で入力してください', }, ], validateWhen: InputMan.ValidateWhen.Typing, }, { control: password1, ruleSet: [ { rule: InputMan.ValidateType.GreaterThanOrEqualToInputLength, inputLength: 6, failMessage: '6文字以上に入力してください', }, ], validateWhen: InputMan.ValidateWhen.Typing, }, { control: password2, ruleSet: [ { rule: InputMan.ValidateType.MatchControl, matchControl: password1, failMessage: 'パスワードが一致しません。', }, ], validateWhen: InputMan.ValidateWhen.Typing, }, ], defaultNotify: { fail: { icon: { direction: GC.InputMan.IconDirection.Outside } }, }, }); validator.validate(); validator.addEventListener(InputMan.GcValidatorEvent.ValidationFailed, (s, e) => { const stepIndex = [, username, mail, phone, password1, password2].findIndex((v) => v === e.control); gctour.steps[stepIndex].message = `<div style="color: red;">${e.context.failMessage}<div>`; gctour.steps[stepIndex].disableNextButton = true; }); validator.addEventListener(InputMan.GcValidatorEvent.ValidationSucceeded, (s, e) => { const stepIndex = [, username, mail, phone, password1, password2].findIndex((v) => v === e.control); gctour.steps[stepIndex].message = ''; gctour.steps[stepIndex].disableNextButton = false; }); const gctour = new InputMan.GcTour({ steps: [ { title: 'アカウント登録フォームへようこそ', description: '会員情報を入力し、送信してください。', }, { target: username, title: 'お名前', description: 'お名前を入力してください', }, { target: mail, title: 'メールアドレス', description: '正しいメールアドレス形式で入力してください', }, { target: phone, title: '電話番号', description: '正しい電話番号形式で入力してください', }, { target: password1, title: 'パスワード', description: 'パスワードを6文字以上に入力してください', }, { target: password2, title: 'パスワードの確認', description: 'パスワードと同じ内容を入力してください', } ], closeOnEsc: true, tipShowCloseButton: true, closeOnOverlayClick: true, }); document.getElementById('start-tour').onclick = function () { gctour.start(true); }; setTimeout(() => { gctour.start(); validator.validate(); }, 1000);
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>ツアーコンポーネント - 検証</title> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> window.onload = function () { System.import('./src/app'); } </script> </head> <body> <div class="container"> <button class="tour-start-btn" id="start-tour">フォーム入力ガイドを開始</button> </div> <div id="form1"> <div class="form-group"> <label for="username">名前<span class="required">*</span></label> <input id="username" /> </div> <div class="form-group"> <label for="mail">メールアドレス<span class="required">*</span></label> <input id="mail" /> </div> <div class="form-group"> <label>電話番号</label> <input id="phone" /> </div> <div class="form-group"> <label>パスワード<span class="required">*</span></label> <input id="password1" /> </div> <div class="form-group"> <label>パスワードの確認<span class="required">*</span></label> <input id="password2" /> </div> </div> </body> </html>
.tour-start-btn { background: #67c23a; color: white; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; display: block; margin: 30px auto 10px auto; max-width: 250px; font-size: 12px; } #form1 { padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); margin: 20px auto; max-width: 500px; width: 90%; } .form-group { display: grid; grid-template-columns: 150px 1fr; align-items: center; gap: 15px; margin-bottom: 20px; max-width: 400px; margin-left: auto; margin-right: auto; } .form-group label { font-weight: 600; color: #333; font-size: 14px; text-align: left; padding-right: 10px; } .required { color: red; margin-left: 3px; font-weight: bold; } .gcim_watermark_null { color: lightgrey; }
.tour-start-btn { background: #67c23a; color: white; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; display: block; margin: 30px auto 10px auto; max-width: 250px; font-size: 12px; } #form1 { padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); margin: 20px auto; max-width: 500px; width: 90%; } .form-group { display: grid; grid-template-columns: 150px 1fr; align-items: center; gap: 15px; margin-bottom: 20px; max-width: 400px; margin-left: auto; margin-right: auto; } .form-group label { font-weight: 600; color: #333; font-size: 14px; text-align: left; padding-right: 10px; } .required { color: red; margin-left: 3px; font-weight: bold; } [gcim-control-appearance="modern"] .gcim_watermark_null { color: lightgrey; }
System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@mescius/inputman': 'npm:@mescius/inputman/index.js', '@mescius/inputman/CSS': 'npm:@mescius/inputman/CSS', '@mescius/inputman.richtexteditor': 'npm:@mescius/inputman.richtexteditor/index.js', "@mescius/inputman.richtexteditor/CSS": "npm:@mescius/inputman.richtexteditor/CSS", '@mescius/inputman.richtexteditor/JS/plugins/advlist': 'npm:@mescius/inputman.richtexteditor/JS/plugins/advlist/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/all': 'npm:@mescius/inputman.richtexteditor/JS/plugins/all/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/autosave': 'npm:@mescius/inputman.richtexteditor/JS/plugins/autosave/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/charmap': 'npm:@mescius/inputman.richtexteditor/JS/plugins/charmap/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/directionality': 'npm:@mescius/inputman.richtexteditor/JS/plugins/directionality/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/emoticons': 'npm:@mescius/inputman.richtexteditor/JS/plugins/emoticons/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/fullscreen': 'npm:@mescius/inputman.richtexteditor/JS/plugins/fullscreen/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/htmlcode': 'npm:@mescius/inputman.richtexteditor/JS/plugins/htmlcode/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/image': 'npm:@mescius/inputman.richtexteditor/JS/plugins/image/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/link': 'npm:@mescius/inputman.richtexteditor/JS/plugins/link/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/lists': 'npm:@mescius/inputman.richtexteditor/JS/plugins/lists/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/media': 'npm:@mescius/inputman.richtexteditor/JS/plugins/media/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/pagebreak': 'npm:@mescius/inputman.richtexteditor/JS/plugins/pagebreak/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/preview': 'npm:@mescius/inputman.richtexteditor/JS/plugins/preview/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/save': 'npm:@mescius/inputman.richtexteditor/JS/plugins/save/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/searchreplace': 'npm:@mescius/inputman.richtexteditor/JS/plugins/searchreplace/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/table': 'npm:@mescius/inputman.richtexteditor/JS/plugins/table/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/template': 'npm:@mescius/inputman.richtexteditor/JS/plugins/template/plugin.js', '@mescius/inputman.richtexteditor/JS/plugins/wordcount': 'npm:@mescius/inputman.richtexteditor/JS/plugins/wordcount/plugin.js', '@mescius/inputman.comment': 'npm:@mescius/inputman.comment/index.js', '@mescius/inputman.comment/CSS': 'npm:@mescius/inputman.comment/CSS', '@mescius/wijmo': 'npm:@mescius/wijmo/index.js', '@mescius/wijmo.styles': 'npm:@mescius/wijmo.styles', '@mescius/wijmo.cultures': 'npm:@mescius/wijmo.cultures', '@mescius/wijmo.input': 'npm:@mescius/wijmo.input/index.js', '@mescius/wijmo.grid': 'npm:@mescius/wijmo.grid/index.js', '@mescius/wijmo.nav': 'npm:@mescius/wijmo.nav/index.js', '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets/styles': 'npm:@mescius/spread-sheets/styles', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, } });