Asp.net-validators
ASP.NET-バリデーター
ASP.NET検証コントロールは、ユーザー入力データを検証して、役に立たない、認証されていない、または矛盾するデータが保存されないようにします。
ASP.NETは、次の検証コントロールを提供します。
- RequiredFieldValidator
- RangeValidator
- CompareValidator
- RegularExpressionValidator
- CustomValidator
- 検証サマリー
BaseValidatorクラス
検証制御クラスはBaseValidatorクラスから継承されるため、プロパティとメソッドを継承します。 したがって、すべての検証コントロールに共通するこの基本クラスのプロパティとメソッドを確認すると役立ちます。
Members | Description |
---|---|
ControlToValidate | Indicates the input control to validate. |
Display | Indicates how the error message is shown. |
EnableClientScript | Indicates whether client side validation will take. |
Enabled | Enables or disables the validator. |
ErrorMessage | Indicates error string. |
Text | Error text to be shown if validation fails. |
IsValid | Indicates whether the value of the control is valid. |
SetFocusOnError | It indicates whether in case of an invalid control, the focus should switch to the related input control. |
ValidationGroup | The logical group of multiple validators, where this control belongs. |
Validate() | This method revalidates the control and updates the IsValid property. |
RequiredFieldValidatorコントロール
RequiredFieldValidatorコントロールは、必須フィールドが空でないことを保証します。 通常は、テキストボックスに入力を強制するためにテキストボックスに関連付けられています。
コントロールの構文は次のとおりです。
<asp:RequiredFieldValidator ID="rfvcandidate"
runat="server" ControlToValidate ="ddlcandidate"
ErrorMessage="Please choose a candidate"
InitialValue="Please choose a candidate">
</asp:RequiredFieldValidator>
RangeValidatorコントロール
RangeValidatorコントロールは、入力値が所定の範囲内にあることを確認します。
次の3つの特定のプロパティがあります。
Properties | Description |
---|---|
Type | It defines the type of the data. The available values are: Currency, Date, Double, Integer, and String. |
MinimumValue | It specifies the minimum value of the range. |
MaximumValue | It specifies the maximum value of the range. |
コントロールの構文は次のとおりです。
<asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass"
ErrorMessage="Enter your class (6 - 12)" MaximumValue="12"
MinimumValue="6" Type="Integer">
</asp:RangeValidator>
CompareValidatorコントロール
CompareValidatorコントロールは、1つのコントロールの値を固定値または別のコントロールの値と比較します。
次の特定のプロパティがあります。
Properties | Description |
---|---|
Type | It specifies the data type. |
ControlToCompare | It specifies the value of the input control to compare with. |
ValueToCompare | It specifies the constant value to compare with. |
Operator | It specifies the comparison operator, the available values are: Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and DataTypeCheck. |
コントロールの基本的な構文は次のとおりです。
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="CompareValidator">
</asp:CompareValidator>
RegularExpressionValidator
RegularExpressionValidatorを使用すると、正規表現のパターンと照合して入力テキストを検証できます。 正規表現はValidationExpressionプロパティで設定されます。
次の表は、正規表現で一般的に使用される構文構成をまとめたものです。
Character Escapes | Description |
---|---|
\b | Matches a backspace. |
\t | Matches a tab. |
\r | Matches a carriage return. |
\v | Matches a vertical tab. |
\f | Matches a form feed. |
\n | Matches a new line. |
\ | Escape character. |
単一文字の一致とは別に、メタ文字と呼ばれる一致できる文字のクラスを指定できます。
Metacharacters | Description |
---|---|
. | Matches any character except \n. |
[abcd] | Matches any character in the set. |
[^abcd] | Excludes any character in the set. |
[2-7a-mA-M] | Matches any character specified in the range. |
\w | Matches any alphanumeric character and underscore. |
\W | Matches any non-word character. |
\s | Matches whitespace characters like, space, tab, new line etc. |
\S | Matches any non-whitespace character. |
\d | Matches any decimal character. |
\D | Matches any non-decimal character. |
数量詞を追加して、文字が表示される回数を指定できます。
Quantifier | Description |
---|---|
* | Zero or more matches. |
+ | One or more matches. |
? | Zero or one matches. |
{N} | N matches. |
\{N,} | N or more matches. |
\{N,M} | Between N and M matches. |
コントロールの構文は次のとおりです。
<asp:RegularExpressionValidator ID="string" runat="server" ErrorMessage="string"
ValidationExpression="string" ValidationGroup="string">
</asp:RegularExpressionValidator>
CustomValidator
CustomValidatorコントロールを使用すると、クライアント側とサーバー側の両方の検証用に、アプリケーション固有のカスタム検証ルーチンを作成できます。
クライアント側の検証は、ClientValidationFunctionプロパティを介して行われます。 クライアント側の検証ルーチンは、ブラウザが理解できるJavaScriptやVBScriptなどのスクリプト言語で作成する必要があります。
サーバー側の検証ルーチンは、コントロールのServerValidateイベントハンドラーから呼び出す必要があります。 サーバー側の検証ルーチンは、C#やVB.Netなどの.Net言語で作成する必要があります。
コントロールの基本的な構文は次のとおりです。
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction=.cvf_func. ErrorMessage="CustomValidator">
</asp:CustomValidator>
検証サマリー
ValidationSummaryコントロールは検証を実行しませんが、ページ内のすべてのエラーの概要を表示します。 概要には、検証に失敗したすべての検証コントロールのErrorMessageプロパティの値が表示されます。
次の2つの相互に包括的なプロパティは、エラーメッセージをリストします。
- ShowSummary :指定された形式でエラーメッセージを表示します。
- ShowMessageBox :別のウィンドウにエラーメッセージを表示します。
コントロールの構文は次のとおりです。
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
DisplayMode = "BulletList" ShowSummary = "true" HeaderText="Errors:"/>
検証グループ
複雑なページには、さまざまなパネルで提供されるさまざまな情報グループがあります。 そのような状況では、個別のグループに対して個別に検証を実行する必要が生じる場合があります。 このような状況は、検証グループを使用して処理されます。
検証グループを作成するには、_ValidationGroup_プロパティを設定して、入力コントロールと検証コントロールを同じ論理グループに配置する必要があります。
例
次の例では、校長を選出するために、4人の家に分割された学校のすべての生徒が記入するフォームについて説明します。 ここでは、検証コントロールを使用してユーザー入力を検証します。
これは、デザインビューのフォームです。
コンテンツファイルのコードは次のとおりです。
<form id="form1" runat="server">
<table style="width: 66%;">
<tr>
<td class="style1" colspan="3" align="center">
<asp:Label ID="lblmsg"
Text="President Election Form : Choose your president"
runat="server"/>
</td>
</tr>
<tr>
<td class="style3">
Candidate:
</td>
<td class="style2">
<asp:DropDownList ID="ddlcandidate" runat="server" style="width:239px">
<asp:ListItem>Please Choose a Candidate</asp:ListItem>
<asp:ListItem>M H Kabir</asp:ListItem>
<asp:ListItem>Steve Taylor</asp:ListItem>
<asp:ListItem>John Abraham</asp:ListItem>
<asp:ListItem>Venus Williams</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvcandidate"
runat="server" ControlToValidate ="ddlcandidate"
ErrorMessage="Please choose a candidate"
InitialValue="Please choose a candidate">
</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
House:
</td>
<td class="style2">
<asp:RadioButtonList ID="rblhouse" runat="server" RepeatLayout="Flow">
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Yellow</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvhouse" runat="server"
ControlToValidate="rblhouse" ErrorMessage="Enter your house name" >
</asp:RequiredFieldValidator>
<br/>
</td>
</tr>
<tr>
<td class="style3">
Class:
</td>
<td class="style2">
<asp:TextBox ID="txtclass" runat="server"></asp:TextBox>
</td>
<td>
<asp:RangeValidator ID="rvclass"
runat="server" ControlToValidate="txtclass"
ErrorMessage="Enter your class (6 - 12)" MaximumValue="12"
MinimumValue="6" Type="Integer">
</asp:RangeValidator>
</td>
</tr>
<tr>
<td class="style3">
Email:
</td>
<td class="style2">
<asp:TextBox ID="txtemail" runat="server" style="width:250px">
</asp:TextBox>
</td>
<td>
<asp:RegularExpressionValidator ID="remail" runat="server"
ControlToValidate="txtemail" ErrorMessage="Enter your email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style3" align="center" colspan="3">
<asp:Button ID="btnsubmit" runat="server" onclick="btnsubmit_Click"
style="text-align: center" Text="Submit" style="width:140px"/>
</td>
</tr>
</table>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
DisplayMode ="BulletList" ShowSummary ="true" HeaderText="Errors:"/>
</form>
送信ボタンの背後にあるコード:
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblmsg.Text = "Thank You";
}
else
{
lblmsg.Text = "Fill up all the fields";
}
}