the onKeyUp event of the java script influences the function of the CustomValidator

1

I have a question, the onKeyUp event of the java script influences the ClientValidationFunction property of the CustomValidator.   I'm maintaining a page here in the company, and I need to validate a textbox field to accept only number or empty before saving on base. but if this field did not respect the rule was asked to put the border in red. I can do the validation of the numbers with a javascript function to leave only the numbers in both the onBlur, onKeyUp, onkeypress and onChange event, but to paint the control border with the CustomValidator only works when I take the onkeyUp. Is it possible to use this java script event with CustomValidator?

    
asked by anonymous 14.12.2015 / 17:39

2 answers

1

ASP.NET Web Forms internally uses a framework located in aspnet_client\{0}\{1} to do validation, etc.

They are basically determined from the ClientScriptsLocation.

Try replacing the default function, add the extra line to set the control_to_validate color.

document.getElmentById(val.controltovalidate).style.border='1px solid red';

ASP.NET

<asp:TextBox ID="txtFirstName" runat="server" CausesValidation="true" MaxLength="60"
    CssClass="standard_width" />
<asp:RequiredFieldValidator ControlToValidate="txtFirstName" runat="server" ID="valFirstName" ValidationGroup="grpRegistration" ErrorMessage="First Name is required." Text="*" />
<asp:Button Text="Super" ID="btnSubmit" CausesValidation="true" runat="server" />

JavaScript

<script type="text/javascript">
    function ValidatorUpdateDisplay(val) {
        if (typeof (val.display) == "string") {
            if (val.display == "None") {
                return;
            }
            if (val.display == "Dynamic") {
                val.style.display = val.isvalid ? "none" : "inline";
                return;
            }

        }
        val.style.visibility = val.isvalid ? "hidden" : "visible";
        if (val.isvalid) {
            document.getElementById(val.controltovalidate).style.border = '1px solid #333';
        }
        else {
            document.getElementById(val.controltovalidate).style.border = '1px solid red';
        }          
    }
</script>
    
14.12.2015 / 17:50
1

So I'm using the components called CustomValidator and UpdatePanel, remembering that you have some rules to insert UpdatePanel, if you need more details just ask: WebForm1.aspx:

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" >

    <ContentTemplate>
        <asp:TextBox runat="server" AutoPostBack ="true" ID="dataini"></asp:TextBox>
        <asp:TextBox runat="server" AutoPostBack ="true" ID="datafim" OnTextChanged="datafim_TextChanged"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"  OnServerValidate="CustomValidator1_ServerValidate" ></asp:CustomValidator>
     </ContentTemplate>   

   </asp:UpdatePanel>
</div>

here is no behind: WebForm1.aspx.cs

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        DateTime dtini = Convert.ToDateTime(dataini.Text);
        DateTime dtfim = Convert.ToDateTime(datafim.Text);
        if (dtini < dtfim)
        {
            CustomValidator1.Text = "Correto";
            CustomValidator1.IsValid = true;

        }
        else
        {
            CustomValidator1.IsValid = false;
            CustomValidator1.Text = "Incorreto";
        }
    }

    protected void datafim_TextChanged(object sender, EventArgs e)
    {
        ServerValidateEventArgs svea = new ServerValidateEventArgs("",true);
        CustomValidator1_ServerValidate(sender,svea);
    }
    
14.12.2015 / 17:52