1

and I am a beginner in using asp.net and C# in visual studio 2008

I have a TextBox with the ID = LimitAmount, it is supposed to accepts input of type DOUBLE, therefore I made a CompareValidator (CompareValidatorAmount) to control this, now I want to get the TextBox Cleared after an invalid input type.

thank you !

4

3 回答 3

2

使用自定义验证器:

  <asp:CustomValidator id="CustomValidator1"
       ControlToValidate="LimitAmount"
       OnServerValidate="ServerValidation"
       ErrorMessage="This field requires a number"
       ForeColor="red"
       runat="server"/>         

在后面的代码中:

  void ServerValidation(object source, ServerValidateEventArgs args)
  {
     double tmp;
     if(double.TryParse(args.Value, out tmp))
     {
        args.IsValid = true;
     }
     else
     {
        args.IsValid = false;
        LimitAmount.text = string.empty;
     }
  }

如果你愿意,你也可以在 Javascript 中使用ClientValidationFunction

于 2013-04-18T09:12:40.790 回答
1

请尝试使用 jQuery:

<table style="width: 100%;">
            <tr>
                <td style="width: 30%;">
                    <asp:TextBox ID="TextBox1" runat="server" onblur="return BtnClick();"></asp:TextBox>
                </td>
                <td style="width: 70%;">
                </td>
            </tr>
            <tr>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" onblur="return BtnClick();"></asp:TextBox>
                </td>
                <td>
                    <asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator"
                        ControlToCompare="TextBox1" ControlToValidate="TextBox2"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return BtnClick();" />
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
        </table>

和脚本

<script type="text/javascript">
    function BtnClick() {
        var val = Page_ClientValidate();
        if (!val) {
            var i = 0;
            for (; i < Page_Validators.length; i++) {
                if (!Page_Validators[i].isvalid) {
                    $("#" + Page_Validators[i].controltovalidate)
                    .val('');
                }
            }
        }
        return val;
    }
</script>
于 2013-04-18T09:23:44.113 回答
0

试试这个............当验证被调用时

LimitAmount.text = string.empty ;
于 2013-04-18T09:03:56.437 回答