開発環境
- OS X Lion - Apple(OS)
- Safari、Firefox + Firebug (Webプラウザ、プラグイン)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:JavaScript
- JavaScript Library: jQuery
『初めてのJavaScript 第2版』(シェリー・パワーズ著(Shelley Powers著)、武舎 広幸+武舎 るみ訳、オライリー・ジャパン、2009年、ISBN978-4-87311-425-5) の8章(フォームと検証)練習問第8-2.を解いてみる。
その他参考書籍
8-2.
コード(BBEdit)
function setupEventsAndFocus( e ) {
$('#name').blur(function(e) {
var txtInput = $('#name').val();
if (txtInput === null || txtInput === "" ){
alert("お名前をご記入ください");
}
});
$('#zip').change(function(e) {
var txtInput = $('#zip').val();
if (! /^\d{3}[-]?\d{4}$/.test( txtInput )) {
alert("郵便番号の形式が違っています");
}
});
$('#address').blur(function(e) {
var txtInput = $('#address').val();
if (/[0-9]/.test( txtInput ) ) {
alert("住所欄に半角数字は利用できません");
}
});
$('#passwd').change(function(e) {
var txtInput = $('#passwd').val();
if (! /^\w{8,}$/.test( txtInput ) ) {
alert("パスワードは8文字以上に設定してください");
}
});
$('#btn0').click(validateForm);
$('#name').focus();
}
function cancelEvent(e) {
var e = e ? e : window.event;
if(e.preventDefault) {
e.preventDefault();
e.stopPropagation();
} else {
e.returnValue = false;
e.cancelBubble = true;
}
}
function validateForm( e ) {
var e = e ? e : window.event,
result = "",
textInputs = $('#someform input'),
i, max;
for (i = 0, max = textInputs.length; i < max; i += 1) {
if ( textInputs[i].type !== "button" ){
result += textInputs[i].value + "\n";
}
}
$('#pre0').text(result);
cancelEvent(e);
}
setupEventsAndFocus();
0 コメント:
コメントを投稿