リアルタイムフォームバリデーションが行えるjQuery Validationを使ってみました。
発生した問題を中心に最後にはASP.NETで工夫した全ソースコード公開です。
jQuery Validationはformへ
ダイアログをdivタグで構成していて、divタグに対して「$(‘#dialog’).validate()」のようにvalidateを指定しても動いてくれません。下記のエラーにさんざん悩まされました。validateはformタグへ行う必要があります。
Uncaught TypeError: Cannot call method 'element' of undefined | jquery.validate.js:106
ダイアログ内をformタグで囲みます。
<div id="dialog">
<form id="form">
<input type="text" name="field" />
</form>
</div>
formタグにvalidateを設定します。
$('#form').validate({
rules: {
field: {
// 必須
required: true,
// 数値のみ
digits: true,
// 最大文字数
maxlength: 9,
// 最小値
min: 1
}
}
});
validメソッドでチェックすることができます。
問題がなければtrueが返ります。
var valid = $('#form').valid();
jQuery Validationのデモ
Openボタンからダイアログを開いて、Sendボタンで動作を確認できます。
[CodePen height=300 show=result href=BAdIq user=iw3 ]
ASP.NET WebフォームでのjQuery Validation
ASP.NETはASPXファイル内にformは1つだけという制約があって、ダイアログ内にformタグを記述しても消えてしまいます。そこでダイアログ内にformは記述せず、jQueryのwrapInner関数を使って動的に囲うことにしました。
こうすることでASP.NETでも複数のダイアログにvalidationを設定できます。
ASPXでjQuery Validationを使ったソースコード
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JQueryUIDialog.aspx.cs" Inherits="propeller.JQueryUIDialog" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/themes/redmond/jquery-ui.css" media="all">
<style>
.error
{
color: Red;
}
</style>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.1.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/jquery-ui.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/localization/messages_ja.js "></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="open1" type="button" value="ダイアログ1" />
<input id="open2" type="button" value="ダイアログ2" />
<div id="dialog1" title="ダイアログ1">
<input type="text" name="field" />
</div>
<div id="dialog2" title="ダイアログ2">
<input type="text" name="field" />
</div>
</div>
</form>
<script>
function createValidateOption() {
return {
rules: {
field: {
// 必須
required: true,
// 数値のみ
digits: true,
// 最大文字数
maxlength: 9,
// 最小値
min: 1
}
}
};
}
$(function () {
// ------------------------------
// ダイアログ1
// ------------------------------
$('#dialog1').wrapInner('<form></form>');
var validator1 = $('#dialog1 form').validate(createValidateOption());
$('#dialog1').dialog({
autoOpen: false,
buttons: {
'Send': function () {
if ($('#dialog1 form').valid()) {
alert('Success');
$('#dialog1').dialog('close');
}
}
}
});
$('#open1').on('click', function () {
validator1.resetForm();
validator1.reset();
$('#dialog1').dialog('open');
});
// ------------------------------
// ダイアログ2
// ------------------------------
$('#dialog2').dialog({
autoOpen: false,
buttons: {
'Send': function () {
if ($('#dialog2 form').valid()) {
alert('Success');
$('#dialog2').dialog('close');
}
}
}
});
$('#open2').on('click', function () {
$('#dialog2').dialog('open');
});
$('#dialog2').wrapInner('<form></form>');
$('#dialog2 form').validate(createValidateOption());
});
</script>
</body>
</html>
利用したCDN
http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.1.min.js
http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/jquery-ui.min.js
http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/themes/redmond/jquery-ui.css
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/localization/messages_ja.js
※ 最新のURLは Microsoft Ajax Content Delivery Network で確認できます。
参考情報
Dialog - jQuery UI API 1.8.4 日本語リファレンス - StackTrace
Just a moment...
GitHub - jquery-validation/jquery-validation: jQuery Validation Plugin library sources
jQuery Validation Plugin library sources. Contribute to jquery-validation/jquery-validation development by creating an a...


コメント