パスワード文字の表示

テキストコントロールが提供するパスワード文字の表示機能について解説します。 入力されたテキストをパスワード文字表示する方法 コンストラクタのuseSystemPasswordCharオプションにtrueを設定するか、setUseSystemPasswordCharメソッドを使用すると、テキストをパスワード文字表示にすることができます。 このとき、パスワード文字は、Windows/iOS/iPadOS環境では「●」で表示され、Mac環境では「*」で表示されます。 また、パスワード文字を自身で設定したい場合は、passwordCharオプションもしくは、setPasswordCharメソッドを利用することもできます。 useSystemPasswordCharとpasswordCharが同時に指定されている時は、useSystemPasswordCharの設定が優先されます。 表示されているパスワード文字のアクション setPasswordRevelationModeメソッドを利用すると、表示されているパスワード文字列のオプションを設定することができます。設定できる値は次のとおりで、既定値はPasswordRevelationMode.Noneです。 PasswordRevelationModeの値 説明 None 通常のパスワード文字表示が行われます ShowEyeButton パスワード表示アイコンが表示されるようになり、クリックしている間だけテキストを表示します ShowLastTypedChar 最後に入力した文字を、入力から1秒後にパスワード文字表示します なお、PasswordRevelationMode.ShowEyeButtonの場合、パスワード入力中にコントロールからフォーカスを外し、再度コントロールにフォーカスすると、パスワード表示アイコンは表示されなくなります。これは、セキュリティのために設計された動作です。 入力されているパスワードを全て削除するか、入力されているパスワードを全選択して上書き入力すると、パスワード表示アイコンを再表示されます。
import '@grapecity/inputman/CSS/gc.inputman-js.css'; import { InputMan } from '@grapecity/inputman'; const gcTextBox1 = new InputMan.GcTextBox(document.getElementById('gcTextBox1'), { passwordChar : '●', passwordRevelationMode:InputMan.PasswordRevelationMode.None, }); const gcTextBox2 = new InputMan.GcTextBox(document.getElementById('gcTextBox2'), { passwordChar : '●', passwordRevelationMode:InputMan.PasswordRevelationMode.ShowEyeButton, }); const gcTextBox3 = new InputMan.GcTextBox(document.getElementById('gcTextBox3'), { passwordChar : '●', passwordRevelationMode:InputMan.PasswordRevelationMode.ShowLastTypedChar, }); const gcTextBox4 = new InputMan.GcTextBox(document.getElementById('gcTextBox4'), { passwordChar : '●', passwordRevelationMode:InputMan.PasswordRevelationMode.None, }); document.getElementById('showPassword').addEventListener('click', (e) => { gcTextBox4.setPasswordChar(e.target.checked ? passwordString.getText(): '') }); const passwordString = new InputMan.GcTextBox(document.getElementById('setPasswordString'),{ text: '●', maxLength:1 }); document.getElementById('clearPasswords').addEventListener('click', () => { gcTextBox1.setText(''); gcTextBox2.setText(''); gcTextBox3.setText(''); gcTextBox4.setText(''); }); passwordString.onTextChanged(() => { gcTextBox1.setPasswordChar(passwordString.getText()); gcTextBox2.setPasswordChar(passwordString.getText()); gcTextBox3.setPasswordChar(passwordString.getText()); gcTextBox4.setPasswordChar(passwordString.getText()); })
<!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> System.import('./src/app'); </script> </head> <body> <div> 通常のパスワード文字表示(オプションなし)<br> <input id="gcTextBox1"> </div> <br> <div> パスワード表示アイコンをクリックしている間だけテキストを表示する<br> <input id="gcTextBox2"> </div> <br> <div> 入力から1秒後にパスワード文字表示にする<br> <input id="gcTextBox3"><br> </div> <br> <div> チェックボックスでパスワード表示を切り替える<br> <input id="gcTextBox4"> <input type="checkbox" id="showPassword" checked>表示する</label> </div> <br> <button id="clearPasswords">パスワードをクリア</button> <table class="sample"> <tr> <th>パスワード表示に使用する文字</th> <td><input type="text" id="setPasswordString"></td> </tr> </body> </html>
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: { '@grapecity/inputman': 'npm:@grapecity/inputman/index.js', '@grapecity/inputman/CSS': 'npm:@grapecity/inputman/CSS', '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' }, } });