開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Safari、Firefox + Firebug (Webプラウザ、プラグイン)
- JavaScript (プログラミング言語)
- jQuery (JavaScript Library)
Head First JavaScript ―頭とからだで覚えるJavaScriptの基本( Michael Morrison (著), 豊福 剛 (翻訳)、オライリージャパン)の7章(フォームと検証)、自分で考えてみよう(p.331)を解いてみる。
その他参考書籍
自分で考えてみよう(p.331)
コード(BBEdit)
sample.js
var date = $('#date0'),
date_help = $('#date0_help'),
validateNonEmpty = function (input_field, help_text) {
if (input_field.val().length === 0) {
if (help_text !== null) {
help_text.text('値を入力して下さい');
}
return false
}
if (help_text !== null) {
help_text.text('');
}
return true;
},
validateRegex = function (regex, input_str, help_text, help_message) {
if (!regex.test(input_str)) {
if (help_text !== null) {
help_text.text(help_message);
}
return false;
}
if (help_text !== null) {
help_text.text('');
}
return true;
},
validateDate = function (input_field, help_text) {
if (validateNonEmpty(input_field, help_text)) {
return validateRegex(
/^\d{2}\/\d{2}\/(\d{2}|\d{4})$/, input_field.val(), help_text,
'日付はMM/DD/YYYY形式で入力してください');
}
return false;
};
date.blur(function(event) {
validateDate(date, date_help);
});
0 コメント:
コメントを投稿