如何使用asp.net对文本框进行验证

时间:2022-05-19 06:31:33

I have a Textbox on my webform, for instance 2012 entered in single text box the following text box must enter 2014 or else should give error.Iam trying to validate it by using compare validation but am unable to meet the exact condition what i want.Can i know how can it be done?thnks in advance

我的webform上有一个Textbox,例如2012年在单个文本框中输入以下文本框必须输入2014或者应该给出错误。我试图通过使用比较验证来验证它但是我无法满足我想要的确切条件。我能知道怎么做?提前做好准备

2 个解决方案

#1


0  

I cant fine wha'ts the hard part here...

我不知道这里的难点是什么......

int number=Convert.ToInt32( textBox1.Text);
 if(number==2014)
 Response.Write("good");
 else
 Response.Write("Bad number");

More easy then that?

那更容易吗?

#2


0  

If your validator should ensure that the text in two TextBoxes is equal use a CompareValidator with appropriate ControlToValidate and ControlToCompare:

如果验证器应确保两个TextBox中的文本相同,请使用带有适当ControlToValidate和ControlToCompare的CompareValidator:

<asp:TextBox id="Txt1" runat="server">
</asp:TextBox>
<asp:TextBox id="Txt2" runat="server">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
    ControlToValidate="Txt2"
    ControlToCompare="Txt1"
    ErrorMessage="Text in second textbox must be equal to text in first textbox!">
</asp:CompareValidator>

If you also want to ensure that only integers can be inserted, use DataTypeCheck and Integer:

如果您还想确保只能插入整数,请使用DataTypeCheck和Integer:

<asp:CompareValidator ID="CompareValidator2" runat="server" 
    ControlToValidate="Txt2"
    Type="Integer" Operator="DataTypeCheck"
    ErrorMessage="Text in second textbox must be an integer!">
</asp:CompareValidator>

#1


0  

I cant fine wha'ts the hard part here...

我不知道这里的难点是什么......

int number=Convert.ToInt32( textBox1.Text);
 if(number==2014)
 Response.Write("good");
 else
 Response.Write("Bad number");

More easy then that?

那更容易吗?

#2


0  

If your validator should ensure that the text in two TextBoxes is equal use a CompareValidator with appropriate ControlToValidate and ControlToCompare:

如果验证器应确保两个TextBox中的文本相同,请使用带有适当ControlToValidate和ControlToCompare的CompareValidator:

<asp:TextBox id="Txt1" runat="server">
</asp:TextBox>
<asp:TextBox id="Txt2" runat="server">
</asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
    ControlToValidate="Txt2"
    ControlToCompare="Txt1"
    ErrorMessage="Text in second textbox must be equal to text in first textbox!">
</asp:CompareValidator>

If you also want to ensure that only integers can be inserted, use DataTypeCheck and Integer:

如果您还想确保只能插入整数,请使用DataTypeCheck和Integer:

<asp:CompareValidator ID="CompareValidator2" runat="server" 
    ControlToValidate="Txt2"
    Type="Integer" Operator="DataTypeCheck"
    ErrorMessage="Text in second textbox must be an integer!">
</asp:CompareValidator>