开发者

Validating textbox values

I have three textboxes: Textbox1, Textbox2, Textbox3

I have to check if a开发者_如何学Pythonny 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.


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>


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.


You need a CustomValidator. Read about it.


var uniqueTextcount =  (new [] { tb1, tb2, tb3 }).Select(tb => tb.Text).Distinct().Count()
if (uniqueTextCount != 3)
  // ARGH!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜