In C# how do you display an error message if both text boxes are empty? Is there a code example or validator that can be used?
A:
RequiredFieldValidator should do the job? if you want to know more about validators then look here http://www.codeproject.com/KB/validation/aspnetvalidation.aspx
Petoj
2009-03-20 12:12:58
we cant use RFV,,the thing is that if both text boxes are empty only ,i will display error message
peter
2009-03-20 12:14:45
You could use a CustomValidator but i have only used one once so i cant tell you how it works..
Petoj
2009-03-20 12:16:08
A:
protected submit_click() { if((TextBox1.Text=="")||(TextBox2.Text=="")) { print that error message } }
sevugarajan
2009-03-20 12:14:17
thanks sevugarajan,,i thought we need some custom validator by using java script,, any way let me see whether it works or not
peter
2009-03-20 12:17:18
A:
if (TextBox1.Text == String.Empty && TextBox2.Text == String.Empty)
{
Label1.Text = "Your error message here";
}
else
{
//continue your logic here
}
Jon
2009-03-20 12:15:55
A:
CustomValidator gives you a callback method. You can use it like any other validator, and write the following code in the [control name]_ServerValidate method:
args.IsValid = TextBox1.Text.Length > 0 && TextBox2.Text.Length > 0;
Rich
2009-03-20 12:20:22
thank you rich,,for that we need to enable validation to true in customvalidator
peter
2009-03-20 12:33:47
A:
Abstract to a scalable function:
private bool Validate(params TextBox[] boxes) {
foreach (var box in boxes)
if (string.IsNullOrEmpty(box.Text))
return false;
return true;
}
then call with
if(Validate(TextBox1, TextBox2, ...)) {
/// Do the good stuff
} else {
/// throw error
}
Unsliced
2009-03-20 12:27:59