[c#]2 文本框上的条件自定义验证

发布时间: 2017/2/25 0:06:20
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我在这里的目标是打造基本上将作出一项或其他文本框中所需的字段。即如果一个文本框具有一定的价值不是 null,空白。或空,想要提交按钮继续。夫复何标准的红色错误停止从 postingback 和发射插入函数提交。我有试着写这,但是到目前为止没有良好。我可以做什么,使这项工作?

 <script type="text/javascript">
                        function validateText(sender, args) {
                            if (args.value !== "") {
                                var textBoxB = document.getElementById('TextBox12');
                                args.IsValid = (TextBox12.value !== "");
                            }
                            return;

                            /*
                            if (!string.IsNullOrEmpty(TextBox12.Text + TextBox13.Text)) {
                               args.IsValid = true;
                            }
                            else {
                                if (string.IsNullOrEmpty(TextBox12.Text) && !string.IsNullOrEmpty(TextBox13.Text)) {
                                    args.IsValid = true;
                                }
                                else if (string.IsNullOrEmpty(TextBox13.Text) && !string.IsNullOrEmpty(TextBox12.Text)) {
                                    args.IsValid = true;
                                }
                                else {
                                    args.IsValid = false;
                                }
                            }
                            */

                        }
                    </script>
                    <div>
                        <div class="left">
                            <asp:Label ID="Label13" runat="server" Text="Advanced Cancellation:"></asp:Label>
                        </div>
                        <div class="right">
                            <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox>
                            <asp:CustomValidator ID="CustomValidator1" runat="server" 
                                ErrorMessage="Required"
                                ValidationGroup='valGroup1' 
                                ClientValidationFunction="validateText"
                                OnServerValidate="ServerValidation"
                                ForeColor="Red"
                                ValidateEmptyText="true">
                            </asp:CustomValidator>
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
                                ErrorMessage="Advanced Cancellation must be an integer." 
                                ControlToValidate="TextBox12"
                                ValidationExpression="^\+?(0|[1-9]\d*)$"
                                ForeColor="Red">
                            </asp:RegularExpressionValidator>

                        </div>
                    </div>
                    <div>
                        <div class="left">
                            <asp:Label ID="Label14" runat="server" Text="Action Required (yyyy-MM-dd):"></asp:Label>
                        </div>
                        <div class="right">
                            <asp:TextBox ID="TextBox13" runat="server"></asp:TextBox>
                            <asp:CustomValidator ID="CustomValidator2" runat="server" 
                                ValidationGroup='valGroup1'
                                ValidateEmptyText="true"
                                ErrorMessage="Required" 
                                ClientValidationFunction="validateText"
                                OnServerValidate="ServerValidation"
                                ForeColor="Red">
                            </asp:CustomValidator>
                            <asp:CompareValidator
                                ID="CompareValidator1" runat="server"
                                Type="Date"
                                Operator="DataTypeCheck"
                                ControlToValidate="TextBox13"
                                ErrorMessage="Please enter a valid date."
                                ForeColor="Red">
                            </asp:CompareValidator>
                        </div>
                    </div>
protected void ServerValidation(object source, ServerValidateEventArgs args)
    {
        if (!string.IsNullOrEmpty(TextBox12.Text))
            args.IsValid = !string.IsNullOrEmpty(TextBox13.Text);

    }

这是到目前为止所。

解决方法 1:

我有删除 javascript 代码。现在你的设计部分是

<div>
    <div class="left">
        <asp:Label ID="Label13" runat="server" Text="Advanced Cancellation:"></asp:Label>
    </div>
    <div class="right">
        <asp:TextBox ID="TextBox12" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server"
            ErrorMessage="Required"
            ValidationGroup="valGroup1"
            OnServerValidate="ServerValidation"
            ForeColor="Red"
            ValidateEmptyText="true">
        </asp:CustomValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
            ErrorMessage="Advanced Cancellation must be an integer."
            ControlToValidate="TextBox12"
            ValidationExpression="^\+?(0|[1-9]\d*)$"
            ForeColor="Red">
        </asp:RegularExpressionValidator>

    </div>
</div>
<div>
    <div class="left">
        <asp:Label ID="Label14" runat="server" Text="Action Required (yyyy-MM-dd):"></asp:Label>
    </div>
    <div class="right">
        <asp:TextBox ID="TextBox13" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator2" runat="server"
            ValidationGroup="valGroup1"
            ValidateEmptyText="true"
            ErrorMessage="Required"
            OnServerValidate="ServerValidation"
            ForeColor="Red">
        </asp:CustomValidator>
        <asp:CompareValidator
            ID="CompareValidator1" runat="server"
            Type="Date"
            Operator="DataTypeCheck"
            ControlToValidate="TextBox13"
            ErrorMessage="Please enter a valid date."
            ForeColor="Red">
        </asp:CompareValidator>
    </div>
</div>
    <div>
        <asp:Button ID="btnok" runat="server" ValidationGroup="valGroup1" />
    </div>

和背后的代码是

protected void ServerValidation(object source, ServerValidateEventArgs args)
    {
        if (!string.IsNullOrEmpty(TextBox12.Text) || !string.IsNullOrEmpty(TextBox13.Text))
            args.IsValid = true;
        else
        { 
            args.IsValid = false;
        }

    }

我测试了它的工作对我来说

官方微信
官方QQ群
31647020