Click here to Skip to main content
15,888,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a textbox who has CausesValidation set to True, TextChange event enabled with corresponding eventhandler in the ServerSide Code, and also a CustomValidator code in client Side JavaScript
XML
<asp:textbox id="txtCustZipcode" size="10" tabindex="9" runat="server"
    MaxLength="10" AutoPostBack="true" OnTextChanged="txtCustZipcode_TextChanged"
    CausesValidation="True"></asp:textbox>
<asp:CustomValidator ID="cstValidate_txtCustZipCode" runat="server"
    ClientValidationFunction="fnzipcodeValidation" ControlToValidate="txtCustZipcode"
    SetFocusOnError="True"></asp:CustomValidator>





C#
function fnzipcodeValidation(source, args)
{
    var strZipCodeValidator = /[&"#+>]/;
    args.IsValid=true;
    if(document.forms[0].txtCustZipcode.value!='')
        {
            if (strZipCodeValidator.test(trim(document.forms[0].txtCustZipcode.value)))
                {
                    alert('ZipCode cannot take &,#,+,> and double quotes, Pls correct');
                    document.forms[0].txtCustZipcode.focus()
                    document.forms[0].txtCustZipcode.select()
                    args.IsValid=false;
                    return false;
                }
        }
}


The thing is, even if the args.IsValid=false, the Server Side TextChanged event occurs.

Okay, I tried this on the Server Side EventHandler:
VB
Protected Sub txtCustZipcode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustZipcode.TextChanged
       cstValidate_txtCustZipCode.Validate()
  If cstValidate_txtCustZipCode.IsValid Then
'Do Something
  End If
End Sub


This works fine, but the trouble with this approach is: did you notice the alert message on the fnZipCodeValidation??? That fires almost four times!

Any clever, simple solution to over come this???
Posted
Comments
lodlaiden 31-Oct-11 12:23pm    
Your listening to the "TextChanged" event, which is a frequent event.
You may want to have your code firing on a LostFocus or Leave event.

Web code, by the nature of the platform, is less tightly bound to the user.

I'm working through the same issue with the custom validator posting to the server even though the javascript function is failing validation.
If you found an answer, I'd like to see it.

Lodlaiden

1 solution

in load event put this code

if(!ispostBack)
{
// put your load event code here
}
<pre lang="c#">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900