Validate field with CustomValidator

1

I have a field that is populated with an automatic date, this field is validated with customValidator so that the date is not empty, the problem when the field is validated with empty and I click on some control of the page that Postback, validation be lost It is possible for every page post to be validated by customValidator

The following is the code for the control:

<asp:Label ID="lblDtInventario" CssClass="FormatacaoTextos" Style="width: 85px; margin-top: 3px;" Text="Dt. Inventário:" runat="server" />                    
<asp:TextBox runat="server" ID="txtDtInventario" SkinID="CampoData" Style="float: left;"></asp:TextBox>
&nbsp;
<asp:CustomValidator runat="server" ID="CustomValidator" ControlToValidate="txtDtInventario" ErrorMessage=" " Display="Dynamic" ClientValidationFunction="ValidarDataInventario" ValidateEmptyText="true" ValidationGroup="vgObrigarCampo"></asp:CustomValidator>

The following is the validator code:

function ValidarDataInventario(src,args) {
    args.IsValid = document.getElementById("ctl00_ContentPlaceHolder1_txtDtInventario").value == "" ? false : true;
};
    
asked by anonymous 15.01.2016 / 12:36

1 answer

0

I solved my problem of validating the field after giving postblack on the page.

It was done in javascript.

$(document).ready(function () {
        ValidarDataInventario();
    });

    function ValidarDataInventario() {
        if (document.getElementById("ctl00_ContentPlaceHolder1_txtDtInventario") != null) {

            if (document.getElementById("ctl00_ContentPlaceHolder1_txtDtInventario").value == "") {
                $('#' + 'ctl00_ContentPlaceHolder1_txtDtInventario').addClass('validationError'); 
            }
            else {
                $('#' + 'ctl00_ContentPlaceHolder1_txtDtInventario').removeClass('validationError'); 
            }
        }
    }

 <asp:TextBox runat="server" ID="txtDtInventario" SkinID="CampoData"  
                                Style="float: left;" onchange="javascript:ValidarDataInventario();" ></asp:TextBox> 
    
19.01.2016 / 13:43