tags:

views:

132

answers:

6

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
we cant use RFV,,the thing is that if both text boxes are empty only ,i will display error message
peter
You could use a CustomValidator but i have only used one once so i cant tell you how it works..
Petoj
A: 

protected submit_click() { if((TextBox1.Text=="")||(TextBox2.Text=="")) { print that error message } }

sevugarajan
thanks sevugarajan,,i thought we need some custom validator by using java script,, any way let me see whether it works or not
peter
A: 
if (TextBox1.Text == String.Empty && TextBox2.Text == String.Empty)
{
Label1.Text = "Your error message here";
}
else
{
//continue your logic here
}
Jon
thanks Jon for your answer,,
peter
+1  A: 

You will need to use a CustomValidator.

Aleris
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
thank you rich,,for that we need to enable validation to true in customvalidator
peter
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
thanks for your valuable comments
peter