views:

58

answers:

4

I have three textboxes: Textbox1, Textbox2, Textbox3

I have to check if any of the values are same in all the three.

ex: I have 1 as value in one textbox. I cannot have 1 in the other two textboxes.

I am using the textboxes to input ids. If I enter duplicate id's (for example I enter 1 in Textbox1 and Textbox2 / Textbox3 ), the program should give me a message.

A: 
var uniqueTextcount =  (new [] { tb1, tb2, tb3 }).Select(tb => tb.Text).Distinct().Count()
if (uniqueTextCount != 3)
  // ARGH!
flq
i am not using linq
+1  A: 

You can use a CustomValidator with an additional ClientValidation-Function.

Something like this:

<script type="text/javascript" >
    function ClientValidate(sender, args){
        var Textbox1=document.getElementById('<%=Textbox1.ClientID%>');
        var Textbox2=document.getElementById('<%=Textbox2.ClientID%>');
        var Textbox3=document.getElementById('<%=Textbox3.ClientID%>');
        if(Textbox1!=null && Textbox2!=null && Textbox3!= null){
            args.IsValid = !(Textbox1.value==Textbox2.value || Textbox1.value==Textbox3.value || Textbox2.value==Textbox3.value);
        }
        return;
    }
</script>
Tim Schmelter
+1  A: 

You need a CustomValidator. Read about it.

Bryan
+2  A: 

If you are not using LINQ then the long hand way of doing it on the server could be:

string tb1 = Textbox1.Text.Trim();
string tb2 = Textbox2.Text.Trim();
string tb3 = Textbox3.Text.Trim();

if( tb1 == tb2 || tb1 == tb3 || tb2 == tb3) 
{
    // Do something
}

If you need to do this on the client you could use 3 CompareValidators to do a similar thing.

Andy Rose