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コントロールは、必須フィールドが空でないことを保証します。 通常は、テキストボックスに入力を強制するためにテキストボックスに関連付けられています。
コントロールの構文は次のとおりです。
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. |
コントロールの構文は次のとおりです。
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. |
コントロールの基本的な構文は次のとおりです。
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. |
コントロールの構文は次のとおりです。
CustomValidator
CustomValidatorコントロールを使用すると、クライアント側とサーバー側の両方の検証用に、アプリケーション固有のカスタム検証ルーチンを作成できます。
クライアント側の検証は、ClientValidationFunctionプロパティを介して行われます。 クライアント側の検証ルーチンは、ブラウザが理解できるJavaScriptやVBScriptなどのスクリプト言語で作成する必要があります。
サーバー側の検証ルーチンは、コントロールのServerValidateイベントハンドラーから呼び出す必要があります。 サーバー側の検証ルーチンは、C#やVB.Netなどの.Net言語で作成する必要があります。
コントロールの基本的な構文は次のとおりです。
検証サマリー
ValidationSummaryコントロールは検証を実行しませんが、ページ内のすべてのエラーの概要を表示します。 概要には、検証に失敗したすべての検証コントロールのErrorMessageプロパティの値が表示されます。
次の2つの相互に包括的なプロパティは、エラーメッセージをリストします。
- ShowSummary :指定された形式でエラーメッセージを表示します。
- ShowMessageBox :別のウィンドウにエラーメッセージを表示します。
コントロールの構文は次のとおりです。
検証グループ
複雑なページには、さまざまなパネルで提供されるさまざまな情報グループがあります。 そのような状況では、個別のグループに対して個別に検証を実行する必要が生じる場合があります。 このような状況は、検証グループを使用して処理されます。
検証グループを作成するには、_ValidationGroup_プロパティを設定して、入力コントロールと検証コントロールを同じ論理グループに配置する必要があります。
例
次の例では、校長を選出するために、4人の家に分割された学校のすべての生徒が記入するフォームについて説明します。 ここでは、検証コントロールを使用してユーザー入力を検証します。
これは、デザインビューのフォームです。
コンテンツファイルのコードは次のとおりです。
送信ボタンの背後にあるコード: