使用requiredvalidator的空文本框

时间:2016-11-13 20:38:17

标签: asp.net vb.net

我目前正在使用asp.net(VB)开发系统。我已经应用了所需的验证器,以便从用户那里获得正确的输入。但现在我遇到了一些问题。我还必须允许用户也将文本框留空。然后可以提交页面。因此,如果文本框保留为空或带有值,则表示可以验证为真。

如何解决这个问题?请帮助我。非常感谢

1 个答案:

答案 0 :(得分:0)

使用必需的Validator时,文本将为required。由此得名... 如果您想要更复杂的验证,则需要使用CustomValidator

    <asp:CustomValidator ID="CustomValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Text not long enough" ValidationGroup="myGroup" ClientValidationFunction="myCustomValidation"></asp:CustomValidator>

    <script type="text/javascript">
        function myCustomValidation(oSrc, args) {
            var textboxValue = args.Value;
            if (textboxValue == "") {
                args.IsValid = true;
            } else {
                if (textboxValue.length > 4) {
                    args.IsValid = true;
                } else {
                    args.IsValid = false;
                }                
            }
        }
    </script>