検証コントロールと検証アクションエクステンダを使用することで、不正な文字や範囲外の値の入力があると、入力中にエラーを通知します。正しい入力を行わないとサーバーにデータを送信できません。
using System; public partial class Validate_Validator : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 初期値を設定します。 GcDateRangeValidator1.MinimumDate = DateTime.Today; GcDateRangeValidator1.MaximumDate = DateTime.Today.AddMonths(1); GcDate1.Value = DateTime.Today.AddDays(-1); GcDate2.Value = DateTime.Today.AddDays(7); } } }
Partial Class Validate_Validator Inherits System.Web.UI.Page Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not IsPostBack Then ' 初期値を設定します。 GcDateRangeValidator1.MinimumDate = Date.Today GcDateRangeValidator1.MaximumDate = Date.Today.AddMonths(1) GcDate1.Value = Date.Today.AddDays(-1) GcDate2.Value = Date.Today.AddDays(7) End If End Sub End Class
<%@ page title="" language="C#" masterpagefile="~/MasterPage.master" autoeventwireup="true" inherits="Validate_Validator, App_Web_rrwzlsxq" stylesheettheme="SkinFile" %> <%@ Register assembly="GrapeCity.Web.Input.v100, Version=10.0.4006.2012, Culture=neutral, PublicKeyToken=c3bd7c1dccef5128" namespace="GrapeCity.Web.Input.IMEdit" tagprefix="im_edit" %> <%@ Register assembly="GrapeCity.Web.Input.v100, Version=10.0.4006.2012, Culture=neutral, PublicKeyToken=c3bd7c1dccef5128" namespace="GrapeCity.Web.Input.IMExtenders" tagprefix="im_extenders" %> <%@ Register assembly="GrapeCity.Web.Input.v100, Version=10.0.4006.2012, Culture=neutral, PublicKeyToken=c3bd7c1dccef5128" namespace="GrapeCity.Web.Input.IMValidators" tagprefix="im_validator" %> <%@ Register assembly="GrapeCity.Web.Input.v100, Version=10.0.4006.2012, Culture=neutral, PublicKeyToken=c3bd7c1dccef5128" namespace="GrapeCity.Web.Input.IMDate" tagprefix="im_date" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <table align="center"> <tr> <td class="controlsTd">氏名:</td> <td> <im_validator:GcCharacterTypeValidator ID="GcCharacterTypeValidator1" runat="server" ErrorMessage="" HasLoadFromXml="True" ControlToValidate="GcTextBox1" CharacterSet="K" ValidateOnInput="True"></im_validator:GcCharacterTypeValidator> <im_edit:GcTextBox ID="GcTextBox1" runat="server" HasLoadFromXml="True" Width="200px" Text="ぶどうたろう"></im_edit:GcTextBox> </td> <td class="controlsTd"> (カタカナのみ入力可能です。)</td> </tr> <tr> <td class="controlsTd">パスワード:</td> <td> <im_validator:GcTextLengthValidator ID="GcTextLengthValidator1" runat="server" ControlToValidate="GcTextBox2" HasLoadFromXml="True" LengthAsByte="True" MaximumLength="8" MinimumLength="6" ValidateOnInput="True"></im_validator:GcTextLengthValidator> <im_edit:GcTextBox ID="GcTextBox2" runat="server" HasLoadFromXml="True" Width="200px"></im_edit:GcTextBox> </td> <td class="controlsTd"> (6~8文字の入力が必要です。)</td> </tr> <tr> <td class="controlsTd">到着日:</td> <td> <im_validator:GcDateRangeValidator ID="GcDateRangeValidator1" runat="server" ControlToValidate="GcDate1" HasLoadFromXml="True" ValidateOnInput="True"></im_validator:GcDateRangeValidator> <im_date:GcDate ID="GcDate1" runat="server" HasLoadFromXml="True" Width="200px"></im_date:GcDate> </td> <td class="controlsTd"> (今日以降1カ月以内の日付が入力可能です。)</td> </tr> <tr> <td class="controlsTd">出発日:</td> <td> <im_validator:GcDateDifferenceValidator ID="GcDateDifferenceValidator1" runat="server" ControlToCompare="GcDate1" ControlToValidate="GcDate2" Difference="6" HasLoadFromXml="True" ValidateOnInput="True"></im_validator:GcDateDifferenceValidator> <im_date:GcDate ID="GcDate2" runat="server" HasLoadFromXml="True" Width="200px"></im_date:GcDate> </td> <td class="controlsTd"> (到着日より7日以降の日付は入力できません。)</td> </tr> <tr> <td class="controlsTd">メールアドレス:</td> <td><im_validator:GcForbiddenTextValidator ID="GcForbiddenTextValidator1" runat="server" ControlToValidate="GcTextBox3" HasLoadFromXml="True" ValidateOnInput="True"> <forbiddenlistitem value="docomo.ne.jp" /> <forbiddenlistitem value="ezweb.ne.jp" /> <forbiddenlistitem value="softbank.ne.jp" /> <forbiddenlistitem value="vodafone.ne.jp" /> </im_validator:GcForbiddenTextValidator> <im_edit:GcTextBox ID="GcTextBox3" runat="server" HasLoadFromXml="True" Width="200px" Text="___@docomo.ne.jp"></im_edit:GcTextBox> </td> <td class="controlsTd"> (携帯電話のアドレスは入力できません。)</td> </tr> <tr> <td class="controlsTd">参加人数:</td> <td> <im_validator:GcDataTypeValidator ID="GcDataTypeValidator1" runat="server" ControlToValidate="GcTextBox4" ExpectedType="Integer" HasLoadFromXml="True" ValidateOnInput="True"></im_validator:GcDataTypeValidator> <im_edit:GcTextBox ID="GcTextBox4" runat="server" HasLoadFromXml="True" Text="230.15" Width="200px"></im_edit:GcTextBox> </td> <td class="controlsTd"> (整数型以外は入力できません。)</td> </tr> <tr> <td align="center" colspan="2"> <asp:Button ID="Button2" runat="server" Text="ポストバック確認用" Width="150px" CssClass="setButton" /> </td> </tr> </table> <im_extenders:GcValidatorAction ID="GcValidatorAction1" runat="server"> <validateactionsitem controlid="GcCharacterTypeValidator1" invalidbackcolor="Gold" invalidforecolor="Crimson" type="ColorNotify" /> <validateactionsitem controlid="GcCharacterTypeValidator1" tooltipicon="Error" tooltiptext="全角カタカナで入力してください。" tooltiptitle="不正入力" type="TipNotify" /> <validateactionsitem controlid="GcTextLengthValidator1" invalidbackcolor="LavenderBlush" invalidforecolor="Green" type="ColorNotify" /> <validateactionsitem controlid="GcTextLengthValidator1" tooltipbackcolor="LightGoldenrodYellow" tooltiptext="6文字以上8文字以下で入力してください。" tooltiptitle="パスワードエラー" type="TipNotify" /> <validateactionsitem controlid="GcDateDifferenceValidator1" invalidbackcolor="Beige" invalidforecolor="Blue" type="ColorNotify" /> <validateactionsitem controlid="GcDateDifferenceValidator1" tooltipbackcolor="AliceBlue" tooltipbordercolor="PaleTurquoise" tooltipicon="Warning" tooltiptext="6日以上のご宿泊はご予約できません。 " tooltiptitle="エラー" type="TipNotify" /> <validateactionsitem controlid="GcDateDifferenceValidator1" icontip="日にちをご確認ください。" type="IconNotify" /> <validateactionsitem controlid="GcForbiddenTextValidator1" invalidbackcolor="Honeydew" invalidforecolor="DarkOrange" type="ColorNotify" /> <validateactionsitem controlid="GcForbiddenTextValidator1" icontip="アドレスが無効です。" type="IconNotify" /> <validateactionsitem controlid="GcForbiddenTextValidator1" tooltipbackcolor="Lavender" tooltipbordercolor="Thistle" tooltiptext="携帯電話のアドレスは使用できません。" tooltiptitle="入力エラー" type="TipNotify" /> <validateactionsitem controlid="GcDateRangeValidator1" invalidbackcolor="Cornsilk" invalidforecolor="DarkGoldenrod" type="ColorNotify" /> <validateactionsitem controlid="GcDateRangeValidator1" icontip="入力値を確認してください。" type="IconNotify" /> <validateactionsitem controlid="GcDateRangeValidator1" tooltipbackcolor="Linen" tooltiptext="今日以降1か月以内の日にちを入力してください。" tooltiptitle="エラー" type="TipNotify" /> <validateactionsitem controlid="GcDataTypeValidator1" invalidbackcolor="MediumSeaGreen" invalidforecolor="Bisque" type="ColorNotify" /> <validateactionsitem controlid="GcDataTypeValidator1" icontip="小数の入力はできません。" type="IconNotify" /> <validateactionsitem controlid="GcDataTypeValidator1" tooltipbackcolor="Ivory" tooltipbordercolor="MediumVioletRed" tooltiptext="整数型以外は入力できません。" tooltiptitle="入力エラー" type="TipNotify" /> </im_extenders:GcValidatorAction> </asp:Content>