var v = new Validator(name, element, checkers);
name | 項目名 |
element | 検証対象のテキストボックス |
checkers | チェッククラス(一つでも、複数(配列)でもいい) |
.onValidationFail = function(event){} .onValidationSuccess = function(event){}に、検証失敗・成功時の処理を上書きする。引数はイベントオブジェクト。デフォルト設定は何もしない function() が設定されている。
var v1 = new Validator('項目1',document.getElementById('inp1'), new RequiredChecker()); var v2 = new Validator('項目3',document.getElementById('inp3'), [new NumberChecker(), new RangeChecker(0,100)]);
function RequiredChecker() { var _this = this; _this.check = function(name, text) { if (text == '') { return name + 'は必須項目です'; } return ''; }; } function AlphabetChecker() { var _this = this; _this.check = function(name, text) { if (!text.match(new RegExp('^[a-zA-Z]*$'))) { return name + 'にはアルファベットを入力してください'; } return ''; }; } function NumberChecker() { var _this = this; _this.check = function(name, text) { if (!text.match(new RegExp('^[-+]?[0-9]*$'))) { return name + 'には数値を入力してください'; } return ''; }; } function RangeChecker(min, max) { var _this = this; _this.min = min; _this.max = max; _this.check = function(name, text) { if (text < min || max < text) { return name + 'は' + min + 'から' + max + 'までの数値を入力してください'; } return ''; }; }
[source] [reload] |
_this.showErrorTips = function(message) { _this.errorTipTimeout = (new Date()).getTime() + 3000; _this.errTips.innerHTML = '<a href=\"#\" class=\"tooltipf\" tabindex="-1">×<span>' + message + '</span></a>'; setTimeout( function(){ if (_this.errorTipTimeout < (new Date()).getTime()) { _this.errTips.innerHTML = '<a href=\"#\" class=\"tooltip\" tabindex="-1">×<span>' + message + '</span></a>'; } },3200); }タイムアウト時刻(_this.errorTipTimeout?)を 3000ms 後に設定して、3200ms 後にエラーメッセージの切り替えを遅延実行する。