Validate field with Customvalidator

Asked

Viewed 100 times

1

I have a field that is filled 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, the validation is lost. It is possible for each page post the field to be validated by customValidator

Follow the control code:

<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>

Follow the validator code:

function ValidarDataInventario(src,args) {
    args.IsValid = document.getElementById("ctl00_ContentPlaceHolder1_txtDtInventario").value == "" ? false : true;
};
  • you are using updatepanel?

  • I’m not using the updatepanel

  • Friend does so, changes Validateemptytext="true" to false and controls the validation only in your Function JS.

  • Rboscini, his suggestion did not work, now he stopped doing the validation by switching from true to false. thanks for the tip.

1 answer

0


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

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> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.