<asp:CustomValidator>
各テキストボックスのコントロールをお勧めします。カスタム検証ルーチンには、次のようなものを使用できます。
var textBox1IsValid = function textBox1IsValid(sender, args) {
var tb = document.getElementById('TextBox1'),
resultOfValidation = false;
//do validation, modifying resultOfValidation as needed.
arg.IsValid = resultOfValidation;
return resultOfValidation;
},
textBox2IsValid = function textBox2IsValid(sender, args) {
var tb = document.getElementById('TextBox2'),
resultOfValidation = false;
//do validation, modifying resultOfValidation as needed.
//return either the validity of TextBox2
//or (if TextBox1 is not valid) return true so the
//validator for TextBox2 doesn't fire.
arg.IsValid = resultOfValidation || !textBox1IsValid(sender, args);
return resultOfValidation;
},
textBox3IsValid = function textBox3IsValid(sender, args) {
var tb = document.getElementById('TextBox3'),
resultOfValidation = false;
//do validation, modifying resultOfValidation as needed.
//return either the validity of TextBox3
//or (if either TextBox1 or TextBox2 is not valid) return
//true so the validator for TextBox3 doesn't fire.
arg.IsValid = resultOfValidation || !textBox1IsValid(sender, args) || !textBox2IsValid(sender, args);
return resultOfValidation;
};
このアプローチの利点はTextBox2
、TextBox3
内容が有効な場合、または無効な場合に有効として返されるTextBox1
ことです。これにより、すべてのフィールドが有効になるまで、一度に 1 つのバリデータのみが起動されます。また、カスタム検証ルーチンで以下をチェックできるため、もう少し柔軟です。
または必要なその他の検証がすべて 1 つの関数にまとめられています。
欠点は、サーバー側の検証ロジックも複製する必要があることです。