the onKeyUp event of the java script influences the Customvalidator Function

Asked

Viewed 177 times

1

I have a question, the onKeyUp event of the java script influences the Clientvalidationfunction property of the Customvalidator. I am maintaining a page here at the company, and I need to validate a textbox field to accept only number or empty before saving on base. but if this field does not respect the rule it was asked to place the border in red. i manage to validate the numbers with a javascript function to leave only the numbers both in the event onBlur, onKeyUp, onkeypress and onChange, more to paint the edge of the control with the Customvalidator only works when I shoot the onkeyUp. It is possible to use this java script event with Customvalidator?

  • Already used Updatepanel, or is using Updatepanel ?

  • I am not using updatePanel

2 answers

1


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

They are basically determined from the Clientscriptslocation.

Try to replace the default function, includes the additional line to set the color control_to_validate.

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>

1

So I’m using the components called Customvalidator and Updatepanel, remembering that there are 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 at 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);
    }
  • 3

    What is the difference between this response and this one -> http://answall.com/a/103401/129 ? If the problem is the same it would be better to vote to close the question as duplicate.

  • @Sergio the question is very similar, but are different there she leaves free to do in other ways, here he already says that uses the Customvalidator. When my reply has any suggestion of how I should leave?

  • Diego, I have no specific suggestion. If you want to explain the answer better, that would be great. In general questions that may have a duplicate answer should be closed as duplicates. But now you know this :) See you soon.

Browser other questions tagged

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